服务器上的Power BI嵌入错误403禁止错误

时间:2018-10-25 04:38:41

标签: powerbi powerbi-embedded

我正在按照Power BI社区here中提到的步骤进行操作,并在Postman中进行测试,并且已经完成了前4个步骤。但是,在测试Microsoft Power BI Embedded Playground中的嵌入代码时,我不断收到以下错误消息:

> Json Object { "message": "LoadReportFailed", "detailedMessage": "Fail to initialize - Could not resolve cluster", "errorCode": "403", "level": 6, "technicalDetails": { "requestId": "57679585-022b-4871-ae6d-bb722d4a07cc" } }

此外,当尝试使用STEP 5中提供的HTML设置替代方法时,我再次收到403错误消息:“ GET https://wabi-india-west-redirect.analysis.windows.net/powerbi/globalservice/v201606/clusterdetails 403(禁止访问)”

请帮助。我一直在努力使这个东西工作一周以上。

3 个答案:

答案 0 :(得分:1)

请确保您在授权标头中发送了正确的令牌。确保使用“ Bearer”作为授权标头。

还要确保您的用户具有所有必需的权限。

答案 1 :(得分:0)

事实证明,尽管我具有工作区ID,客户端ID,报告ID,并且尽管我对API进行了适当的调用,但第五步中的错误是因为我是我的报告所在的工作区的成员。

要使API正常工作,您需要成为要从中获取报告的工作区的管理员

答案 2 :(得分:0)

我最终使用以下步骤解决了这个问题。

要在前端(角度/ JS)中嵌入power BI报告,您必须生成2个不同的令牌(仅针对应用程序)。

  1. 使用主帐户详细信息/服务主体帐户访问令牌 (用于连接Power BI服务器)
  2. 嵌入令牌(可以使用访问令牌和.NET Power BI rest API生成)。

第一步:访问令牌生成

  

PBIClientId": "xxxxxx-xxx-xxxx-xxxx-xxxxxxxxx",
    "PBIClientSecret": "<client secret>",
    "PBIApiUrl": "https://api.powerbi.com/",
    "ResourceUrl": "https://analysis.windows.net/powerbi/api",
    "AuthorityUrl": "https://login.windows.net/common/",
    "TenantId": "<TenantId>",

private async Task<string> GeneratePowerBIAccessToken()
    {
        var tenantSpecificURL = authorityUrl.Replace("common", tenantId);
        var authenticationContext = new AuthenticationContext(tenantSpecificURL);

        // Authentication using app credentials
        var credential = new ClientCredential(clientId, clientSecret);
        AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(resourceUrl, credential);
        return authenticationResult.AccessToken;
    }

第2步:获取Power BI报表嵌入URL

在步骤1中生成的Pass访问令牌,从Power BI门户URL中获得的groupId和reportId。

 private async Task<EmbedModel> GetEmbedReportModel(string groupId, string reportId, string accessToken)
    {
        string restUrl = "https://api.powerbi.com/v1.0/myorg/groups/" + groupId+"/reports";

        // add JSON to HttpContent object and configure content type
        var method = new HttpMethod("GET");
        var request = new HttpRequestMessage(method, restUrl);

        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);

            // send POST request to Power BI service 
            var jsonResult = await client.SendAsync(request).Result.Content.ReadAsStringAsync();
            var reportObject = JsonConvert.DeserializeObject<ReportObjects>(jsonResult);
            var report = reportObject.value.Where(r => r.id == reportId).FirstOrDefault();
            return new EmbedModel { Id = report.id, EmbedUrl = report.embedUrl, Name = report.name };
        }
    }

the response you will get here is 
    {
  "value": [
    {
      "datasetId": "xxxxxx-abcasdasd-dasda-4weas",
      "id": "3asder78-asds-4d73-1232-5345dfsdfs",
      "name": "SalesMarketing",
      "webUrl": "https://app.powerbi.com/groups/xxx-xxxxx-xxx-xxxxxx-xxxxx/reports/xxxxxx-xxxx-xxx-xxxx-xxxxxxxx",
      "embedUrl": "https://app.powerbi.com/reportEmbed?reportId=xxxxxx-xxxx-xxxx-xxxx-xxxxxx&groupId=aadasda21-xxcx-xx-xx-xxxxxx"
    }
  ]
}

第3步:调用生成嵌入令牌API

 private async  Task<EmbedToken> GenerateEmbedToken(string groupId, string reportId,string accessToken)
    {
        string apiURL = "https://api.powerbi.com/v1.0/myorg/groups/"+ groupId + "/reports/"+ reportId +"/GenerateToken";
        string requestBody = @"{ ""accessLevel"": ""View""}";
        HttpContent postRequestBody = new StringContent(requestBody);
        postRequestBody.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");

        // prepare PATCH request
        var method = new HttpMethod("POST");
        var request = new HttpRequestMessage(method, apiURL);
        request.Content = postRequestBody;

        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);

            // send POST request to Power BI service 
            var jsonResult = await client.SendAsync(request).Result.Content.ReadAsStringAsync();
            EmbedToken embedToken = JsonConvert.DeserializeObject<EmbedToken>(jsonResult);

            return embedToken;
        }
    }

步骤4:在前端应用中使用嵌入令牌和url

<html>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
  <script src="powerbi.min.js" type="text/javascript"> </script>
  <script type="text/javascript">
           window.onload = function () {
           var models = window['powerbi-client'].models;
          var embedConfiguration = {
               type: 'report',
			   id:'537887ee-7c7c-43c6-850b-9293f60aa3f3',
               accessToken: 'H4sIAAAAAAAEAB3Tt66EV.....phVP_a_5F15fA_FCBQAA',
               embedUrl: 'https://app.powerbi.com/reportEmbed?reportId=2376660ee-1c2c-16c6-980b-923260aa3f3&amp;groupId=2adeaaa-xxxx-xxxx-xxxx-8d13b04c6e0e&amp;w=2&amp;config=eyJjbHVzdGVyV.....',
               permissions: models.Permissions.All,
               tokenType: models.TokenType.Embed,
               viewMode:models.ViewMode.View,
           };

           var $reportContainer = $('#dashboardContainer');
           var report = powerbi.embed($reportContainer.get(0), embedConfiguration);

       }
   </script>
   <div id="dashboardContainer"></div>
</html>

  

在运行此之前更改所有值。