为非PBI用户嵌入Power Bi报告

时间:2017-08-28 07:29:21

标签: c# powerbi

我正在尝试使用this link.

提供的示例将强力报告嵌入网页

在此示例中,我已替换了我的专业帐户的所有客户ID和用户名密码。

我在App Registration中设置了Native应用程序类型。当我试图获得Embed Token时,会出现以下错误: 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 个答案:

答案 0 :(得分:1)

我认为您需要添加权限&#34;以登录用户身份访问目录&#34;。
我将向您展示如何为没有Power BI帐户的用户集成我的应用程序的报告和仪表板。

1 - 首先我将应用程序注册为Native app 2 - 在Azure门户的Azure Active Directory中,我授予应用程序的权限: enter image description here

3 - 然后,像这样设置应用程序的权限:

enter image description here

enter image description here

我使用的代码和你一样。

希望它有所帮助。

问候。

相关问题