Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

How to pull data from Web API into PowerBI using Authorization header

I'm trying to pull data from a web Server in PowerBI. The web server uses Oauth and requires you to first create a bearer token by posting to the Authentication endpoint as shown below:

 

curl --location --request POST 'https://<URL>/api/tokens/authenticate' \
--header 'Content-Type: Application/json' \
--header 'Authorization: token <TOKEN>'

 

 

This will return the bearer token which I can then use on subsequent requests to get the data I need into PowerBI. Below is an example request:

 

curl --location --request GET 'http://<URL>/api/current-user' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <BEARER-TOKEN>'

 

 
I know how to construct these queries in PowerQuery however my issue is in the data source methods which PowerBI employees. No matter what I try I either get an error stating that the authorization header can only be used on anonymous data sources or I get an error asking me to specify how to connect which then bring up the web data source page.
 

 

Expression.Error: The 'Authorization' header is only supported when connecting anonymously. These headers can be used with all authentication types: Accept, Accept-Charset, Accept-Encoding, Accept-Language, Cache-Control, Content-Type, If-Modified-Since, Prefer, Referer

 

All the options I've tried have failed thus far I've tried using the Anonymous data source but this fails as it won't authenticate to the base URL i.e. https://something.com/. I've also tried using the Web API option but this fails as it asks for a authentication token name but then doesn't provide any clues on how to provide this?
 
Below is the example query I'm trying to use to get the bearer token and this should work but PowerBi does'nt understand my data source and I'm unsure on how to configure this correctly.

 

let
    accesstoken=#"API Token",
    url=Text.Combine({#"URL","/api/tokens/authenticate"}),    
    Source = Json.Document(Web.Contents(url, [Headers=[#"Content-Type"="application/json", Authorization="token" & accesstoken]])),   
    authentication_data = Source,
    #"Converted to Table" = Record.ToTable(authentication_data)
in 
  #"Converted to Table"

 

 

All my attempts so far to figure this out have failed as all the existing posts of tutorials appear to referance examples which don't apply here (i.e. non-authenticated URLs or different auth methods). Is there something simple I'm doing wrong here or is this just something PowerBi does not support?

  • Anonymous's avatar
    Anonymous
    6 years ago

    So I've found the solution and it was due to a PowerBI expecting and requiring a content in the API request. Not sure why this is required by PowerBI but this is the magic step I was missing. Full simplified example below:

     

    let
        accesstoken = "token " & #"API",
        url=Text.Combine({#"URL", "/api/tokens/authenticate"}),    
        Source = Json.Document( Web.Contents(url, [ Headers = [#"Authorization" = accesstoken], Content = Text.ToBinary("grant_type=client_credentials") ]))
    in 
    	Source

    The Content of "grant_type=client_credentials" seems to be required for PowerBI when handling the authentication yourself in the query.

12 Replies

  • v-lid-msft's avatar
    v-lid-msft
    Community Support

    Hi Anonymous ,

     

    Does there miss a space between Authorization="token" and accesstoken? We think it should be 

     

    Authorization="token " & accesstoken

     

    What is the error shown in Power Query Editor if run the sample query which get the bearer token?


    Best regards,

     

  • v-lid-msft's avatar
    v-lid-msft
    Community Support

    Hi Anonymous ,

     

    How about the result after you follow the suggestions mentioned in my original post?Could you please provide more details about it If it doesn't meet your requirement?


    Best regards,

     

    • Anonymous's avatar
      Anonymous
      Not applicable

       The issue is not with the query itself but good spot on the typo. The problem is PowerBI doesn't seem to understand or accept that I'm trying to formulate my own query and instead insists on asking me to configure the connection source. See linked image:

      https://ibb.co/pzQPpFw 

       

      let
          WebTimeout = #duration(0,0,0,100), 
      
          accesstoken = #"BD API",
          url = Text.Combine({#"BD URL","/api/tokens/authenticate"}),  
      
          WebResponse = Web.Contents(url, 
              [Headers = [Authorization="token " & accesstoken,
                          #"Content-Type"="application/json",
                          Accept="application/json"],
                          Timeout = WebTimeout]),
          
          output = formatfunction(WebResponse) //format the response
      in
          output

       

      The problem is none of the possible options work with my type of application. The options I get given are somewhat limited and I'm unable to customise these: Anonymous, Windows, Basic, Web API, Organizational account.

       

      It feels like there should be setting to just let me manage my own queries without having to tell powerBI how to connect but I've not yer found that option and doubt it exists.

       

      Thanks for your help

      • Anonymous's avatar
        Anonymous
        Not applicable

        So I've found the solution and it was due to a PowerBI expecting and requiring a content in the API request. Not sure why this is required by PowerBI but this is the magic step I was missing. Full simplified example below:

         

        let
            accesstoken = "token " & #"API",
            url=Text.Combine({#"URL", "/api/tokens/authenticate"}),    
            Source = Json.Document( Web.Contents(url, [ Headers = [#"Authorization" = accesstoken], Content = Text.ToBinary("grant_type=client_credentials") ]))
        in 
        	Source

        The Content of "grant_type=client_credentials" seems to be required for PowerBI when handling the authentication yourself in the query.

  • garzamalanUS's avatar
    garzamalanUS
    Frequent Visitor

    I have a similar issue, on the desktop side, all works, I have a function to call the token one time, and then use that token in all the subsequent calls, in Dektop irt refreshes fine. When I publish that, the services asks me to Edit Credentials to the soruces, what credentials? all is within the model, refresh token, token, client id, all that is within the PBI model, how do I configre in the service so it refreshes?