2017-08-28 47 views
1

我試圖嵌入使用this link.嵌入非PBI用戶雙向電力報告

提供在這個樣品的樣品一雙向電力報告到網頁,我已經取代我的親帳戶的所有客戶端ID和用戶名密碼。

我在應用程序註冊中設置了本機應用程序類型。當我試圖讓嵌入令牌,下面的錯誤來了: enter image description here

我已經給下面的權限,以我的應用程序: enter image description here enter image description here

這裏是我的代碼:

public async Task<ActionResult> EmbedReport() 
     { 

      // Create a user password credentials. 
      UserPasswordCredential UserCredentials = new UserPasswordCredential(Username, Password); 
      AuthenticationContext AuthContext = new AuthenticationContext(AuthorityUrl, false); 

      // Authenticate using created credentials 
      //AuthenticationResult AuthResult = await AuthContext.AcquireTokenAsync(ResourceUrl, clientCred); 

      AuthenticationResult AuthResult = await AuthContext.AcquireTokenAsync(ResourceUrl, ClientId, UserCredentials); 

      if (AuthResult == null) 
      { 
       return View(new EmbedConfig() 
       { 
        ErrorMessage = "Authentication Failed." 
       }); 
      } 

      var tokenCredentials = new TokenCredentials(AuthResult.AccessToken, "Bearer"); 

      // Create a Power BI Client object. It will be used to call Power BI APIs. 
      using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials)) 
      { 
       // Get a list of reports. 
       var reports = client.Reports.GetReports(); 

       // Get the first report in the group. 
       var report = reports.Value.FirstOrDefault(); 

       if (report == null) 
       { 
        return View(new EmbedConfig() 
        { 
         ErrorMessage = "Group has no reports." 
        }); 
       } 
       string accessLevel = "View"; 
       // Generate Embed Token. 
       var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel, report.DatasetId,false); 
       var tokenResponse = client.Reports.GenerateToken(report.Id, generateTokenRequestParameters); 


       if (tokenResponse == null) 
       { 
        return View(new EmbedConfig() 
        { 
         ErrorMessage = "Failed to generate embed token." 
        }); 
       } 

       // Generate Embed Configuration. 
       var embedConfig = new EmbedConfig() 
       { 
        EmbedToken = tokenResponse, 
        EmbedUrl = report.EmbedUrl, 
        Id = report.Id 
       }; 

       return View(embedConfig); 
      } 
     } 

回答

1

我認爲您需要添加權限「以登錄用戶身份訪問目錄」。
我將向您展示如何爲我的應用程序爲沒有Power BI帳戶的用戶集成報告和儀表板。

1 - 首先,我已將應用程序註冊爲本機應用程序。
2 - 在Azure的門戶網站,在Azure中的Active Directory,我授予權限的應用程序: enter image description here

3 - 那麼,這樣設置應用程序的權限:

enter image description here

enter image description here

而我使用的代碼和你一樣。

希望它有幫助。

問候。

+0

併爲其他人澄清...您以用戶身份登錄到門戶和「授予權限」作爲您想從應用程序「模擬」到Power BI的用戶。如果您實際上沒有通過GUI公開的AAD應用程序(這將允許您更自然地確認同意),您只需執行此操作。 – Jmoney38

+0

只是另一個有用的參考,因爲這些東西可能很難診斷:https:// github.com/Microsoft/PowerBI-Developer-Samples/issues/19 – Jmoney38