Forum Discussion

gregarican's avatar
gregarican
Frequent Visitor
6 years ago
Solved

Export Reports to File Using SPN Access?

So I am using the Power BI REST API for a small program. Just looking to export a specific report section to PDF file. I am able to acquire an access token, iterate the workspace, iterate the reports, etc. without issue. But when I am attempting to export the report section to file I am hitting a 401 - Unauthorized error.

 

The workspace is accessed via an Azure AD app registration. And the app has full delegated access to all possible areas. Then I've also manually granted admin consent for Tenant.Read.All and Tenant.ReadWrite.All access. 

 

I've looked around various forums for awhile now, but can't come up with anything. Is a service principal able to export reports via the API? 

 

Here are the API request/response pairs from a failed attempt below. Currently on a trial subscription for Power BI Pro. Any suggestions?

 

 

 

 

 

POST https://login.microsoftonline.com/{tenant_id}/oauth2/token HTTP/1.1
Host: login.microsoftonline.com
x-client-SKU: PCL.CoreCLR
x-client-Ver: 5.2.7.0
x-ms-PKeyAuth: 1.0
client-request-id: 72b38185-cfb7-491d-85ee-95d15153613d
return-client-request-id: true
Accept: application/json
Cookie: fpc=ArAwdhYoyGpKsCk-3SopKss; x-ms-gateway-slice=prod; stsservicecookie=ests; esctx=AQABAAAAAAAm-06blBE1TpVMil8KPQ41FDCTHnZhHdcQiSeZDO33ep-6Yl8_W1df5zIfPH7O4syIQikg-BlT8G4ltu_qgENCCW-KGG0KgevkOQCnWzKigGtpteXZGttKiH1NUBEut_seYRV3DRD8syhu36-x9Hfp57jtoTMtYem1YGsI7__RNaLA5uQ13Fgy9gigI6DTLgogAA
Content-Type: application/x-www-form-urlencoded
Content-Length: 211

resource=https%3A%2F%2Fanalysis.windows.net%2Fpowerbi%2Fapi&client_id={client_id}&client_secret={client_secret}%3D&client_info=1&grant_type=client_credentials
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
client-request-id: 72b38185-cfb7-491d-85ee-95d15153613d
x-ms-request-id: c8c66e5b-8904-4829-9c63-da8f0b490200
x-ms-ests-server: 2.1.10709.10 - CHI ProdSlices
x-ms-clitelem: 1,0,0,,
P3P: CP="DSP CUR OTPi IND OTRi ONL FIN"
Set-Cookie: fpc=ArAwdhYoyGpKsCk-3SopKsucyXExAQAAALgFc9YOAAAA; expires=Fri, 10-Jul-2020 16:47:52 GMT; path=/; secure; HttpOnly; SameSite=None
Set-Cookie: x-ms-gateway-slice=prod; path=/; secure; HttpOnly
Set-Cookie: stsservicecookie=ests; path=/; secure; HttpOnly; SameSite=None
Date: Wed, 10 Jun 2020 16:47:52 GMT
Content-Length: 1431

{"token_type":"Bearer","expires_in":"3599","ext_expires_in":"3599","expires_on":"1591811272","not_before":"1591807372","resource":"https://analysis.windows.net/powerbi/api","access_token":"{access_token}"}

------------------------------------------------------------------

GET https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports HTTP/1.1
Host: api.powerbi.com
Authorization: Bearer {access_token}
User-Agent: FxVersion/4.700.19.60701 OSName/Windows OSVersion/Microsoft.Windows.6.1.7601.Service.Pack.1 Microsoft.PowerBI.Api.PowerBIClient/3.11.1.20103


HTTP/1.1 200 OK
Cache-Control: no-store, must-revalidate, no-cache
Pragma: no-cache
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Frame-Options: deny
X-Content-Type-Options: nosniff
RequestId: b82c2cfc-c8da-4f57-b3b5-677a6454d696
OData-Version: 4.0
Access-Control-Expose-Headers: RequestId
request-redirected: true
home-cluster-uri: https://wabi-us-central-a-primary-redirect.analysis.windows.net/
Date: Wed, 10 Jun 2020 16:47:52 GMT
Content-Length: 863

{
  "@odata.context":"http://wabi-us-central-a-primary-redirect.analysis.windows.net/v1.0/myorg/groups/{group_id}/$metadata#reports","value":[
    {
      "id":"{report_id}","reportType":"PowerBIReport","name":"Microsoft Planner Report Pack by FluentPro - AppSource","webUrl":"https://app.powerbi.com/groups/{group_id}/reports/{report_id}","embedUrl":"https://app.powerbi.com/reportEmbed?reportId={report_id}&groupId={group_id}&w=2&config=eyJjbHVzdGVyVXJsIjoiaHR0cHM6Ly9XQUJJLVVTLUNFTlRSQUwtQS1QUklNQVJZLXJlZGlyZWN0LmFuYWx5c2lzLndpbmRvd3MubmV0IiwiZW1iZWRGZWF0dXJlcyI6eyJtb2Rlcm5FbWJlZCI6ZmFsc2V9fQ%3d%3d","isFromPbix":true,"isOwnedByMe":true,"datasetId":"{dataset_id}"
    }
  ]
}

------------------------------------------------------------------

GET https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id} HTTP/1.1
Host: api.powerbi.com
Authorization: Bearer {access_token}
User-Agent: FxVersion/4.700.19.60701 OSName/Windows OSVersion/Microsoft.Windows.6.1.7601.Service.Pack.1 Microsoft.PowerBI.Api.PowerBIClient/3.11.1.20103


HTTP/1.1 200 OK
Cache-Control: no-store, must-revalidate, no-cache
Pragma: no-cache
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Frame-Options: deny
X-Content-Type-Options: nosniff
RequestId: ea7312d3-e185-487a-9090-d3980c312c4e
OData-Version: 4.0
Access-Control-Expose-Headers: RequestId
request-redirected: true
home-cluster-uri: https://wabi-us-central-a-primary-redirect.analysis.windows.net/
Date: Wed, 10 Jun 2020 16:47:53 GMT
Content-Length: 835

{
  "@odata.context":"http://wabi-us-central-a-primary-redirect.analysis.windows.net/v1.0/myorg/groups/{group_id}/$metadata#reports/$entity","id":"{report_id}","reportType":"PowerBIReport","name":"Microsoft Planner Report Pack by FluentPro - AppSource","webUrl":"https://app.powerbi.com/groups/{group_id}/reports/{report_id}","embedUrl":"https://app.powerbi.com/reportEmbed?reportId={report_id}&groupId={group_id}&w=2&config=eyJjbHVzdGVyVXJsIjoiaHR0cHM6Ly9XQUJJLVVTLUNFTlRSQUwtQS1QUklNQVJZLXJlZGlyZWN0LmFuYWx5c2lzLndpbmRvd3MubmV0IiwiZW1iZWRGZWF0dXJlcyI6eyJtb2Rlcm5FbWJlZCI6ZmFsc2V9fQ%3d%3d","isFromPbix":true,"isOwnedByMe":true,"datasetId":"{dataset_id}"
}

------------------------------------------------------------------

GET https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/pages HTTP/1.1
Host: api.powerbi.com
Authorization: Bearer {access_token}
User-Agent: FxVersion/4.700.19.60701 OSName/Windows OSVersion/Microsoft.Windows.6.1.7601.Service.Pack.1 Microsoft.PowerBI.Api.PowerBIClient/3.11.1.20103


HTTP/1.1 200 OK
Cache-Control: no-store, must-revalidate, no-cache
Pragma: no-cache
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Frame-Options: deny
X-Content-Type-Options: nosniff
RequestId: 900428c6-5046-4713-b608-9110f6cb19e0
OData-Version: 4.0
Access-Control-Expose-Headers: RequestId
request-redirected: true
home-cluster-uri: https://wabi-us-central-a-primary-redirect.analysis.windows.net/
Date: Wed, 10 Jun 2020 16:47:53 GMT
Content-Length: 1125

{
  "@odata.context":"http://wabi-us-central-a-primary-redirect.analysis.windows.net/v1.0/myorg/groups/{group_id}/$metadata#pages","value":[
    {
      "Name":"ReportSectionbe63eaf77c6775278e11","displayName":"About","order":7
    },{
      "Name":"ReportSectionb3e816a6cabc39d7b251","displayName":"Task Details","order":9
    },{
      "Name":"ReportSection9324eb90eebeaba6ec26","displayName":"Start Page","order":0
    },{
      "Name":"ReportSection7393471484d7404d9469","displayName":"Portfolio","order":1
    },{
      "Name":"ReportSection03ffed58b5a7048946e0","displayName":"Plan","order":2
    },{
      "Name":"ReportSectionb9f24fd6e59ca319578e","displayName":"Tasks","order":3
    },{
      "Name":"ReportSection5745190d536a0e2e034a","displayName":"Assignments","order":4
    },{
      "Name":"ReportSection526bc56170b16903776a","displayName":"Team","order":5
    },{
      "Name":"ReportSection1d1745690d61ba4602ab","displayName":"User Calendar","order":8
    },{
      "Name":"ReportSectionc3759aaa375b202b371e","displayName":"Data Quality","order":6
    }
  ]
}

------------------------------------------------------------------

POST https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/ExportTo HTTP/1.1
Host: api.powerbi.com
Authorization: Bearer {access_token}
User-Agent: FxVersion/4.700.19.60701 OSName/Windows OSVersion/Microsoft.Windows.6.1.7601.Service.Pack.1 Microsoft.PowerBI.Api.PowerBIClient/3.11.1.20103
Content-Type: application/json; charset=utf-8
Content-Length: 394

{
  "format": "PDF",
  "powerBIReportConfiguration": {
    "settings": {
      "locale": "en-us"
    },
    "pages": [
      {
        "pageName": "ReportSectionbe63eaf77c6775278e11"
      }
    ]
  },
  "paginatedReportConfiguration": {
    "formatSettings": {
      "PageHeight": "14in",
      "PageWidth": "8.5in",
      "StartPage": "1",
      "EndPage": "4"
    }
  }
}
HTTP/1.1 401 Unauthorized
Content-Length: 153
Content-Type: application/json; charset=utf-8
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Frame-Options: deny
X-Content-Type-Options: nosniff
Access-Control-Expose-Headers: RequestId
request-redirected: true
home-cluster-uri: https://wabi-us-central-a-primary-redirect.analysis.windows.net/
RequestId: a9176967-1fbd-4088-8f75-480103f88f68
Date: Wed, 10 Jun 2020 16:47:53 GMT

{"error":{"code":"PowerBINotAuthorizedException","pbi.error":{"code":"PowerBINotAuthorizedException","parameters":{},"details":[],"exceptionCulprit":1}}}

------------------------------------------------------------------

 

 

 

5 Replies

  • gregarican's avatar
    gregarican
    Frequent Visitor

    So I followed the advice of adding the identity specs to my export request, as was described here --> https://community.powerbi.com/t5/Developer/Rest-API-Export-Report-to-PDF-that-uses-Azure-Analysis-Service/m-p/1133666#M23967

     

    Same result (see below). This service principal has full access granted to the Power BI workspace. So I'm at a loss. 😞

     

     

    POST https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/ExportTo HTTP/1.1
    Host: api.powerbi.com
    Authorization: Bearer {access_token}
    User-Agent: FxVersion/4.700.19.60701 OSName/Windows OSVersion/Microsoft.Windows.6.1.7601.Service.Pack.1 Microsoft.PowerBI.Api.PowerBIClient/3.11.1.20103
    Content-Type: application/json; charset=utf-8
    Content-Length: 638
    
    {
      "format": "PDF",
      "powerBIReportConfiguration": {
        "settings": {
          "locale": "en-us"
        },
        "pages": [
          {
            "pageName": "ReportSectionbe63eaf77c6775278e11"
          }
        ],
        "identities": [
          {
            "username": "{spn_object_id}",
            "datasets": [
              "{dataset_id}"
            ],
            "roles": [
              "client"
            ]
          }
        ]
      },
      "paginatedReportConfiguration": {
        "formatSettings": {
          "PageHeight": "14in",
          "PageWidth": "8.5in",
          "StartPage": "1",
          "EndPage": "4"
        }
      }
    }
    HTTP/1.1 401 Unauthorized
    Content-Length: 153
    Content-Type: application/json; charset=utf-8
    Strict-Transport-Security: max-age=31536000; includeSubDomains
    X-Frame-Options: deny
    X-Content-Type-Options: nosniff
    Access-Control-Expose-Headers: RequestId
    request-redirected: true
    home-cluster-uri: https://wabi-us-central-a-primary-redirect.analysis.windows.net/
    RequestId: 6937aa84-e2e1-4cda-a7fe-53c9e28f52a8
    Date: Thu, 11 Jun 2020 17:56:19 GMT
    
    {"error":{"code":"PowerBINotAuthorizedException","pbi.error":{"code":"PowerBINotAuthorizedException","parameters":{},"details":[],"exceptionCulprit":1}}}

     

    • gregarican's avatar
      gregarican
      Frequent Visitor

      Any feedback on this question? I was hoping it was something simple I was overlooking somehow...

      • gregarican's avatar
        gregarican
        Frequent Visitor

        Bumping this again. No one has run into anything similar?

    • gregarican's avatar
      gregarican
      Frequent Visitor

      After opening a ticket with Microsoft support, this is indeed the case. It's just a licensing issue that resulted in what I was experiencing.