Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Gjakova
Post Patron
Post Patron

How to access an REST API with a refreshable token | Translate Postman to Power Query

Hi there,

I have the following question: I need to get access to an API and altough I can get it to work in Postman, I can't seem to figure it out how to run it in Power BI.

 

I have the following info:

API-key: 1938adklw-2020etbw-3abca-9add0-433435 (it is a fictive key...)

 

In Postman I put a POST request with the following URL (also fictive):

https://worksauce.matrix2.de/auth/login/matrix2pbi/apiKey?

And in my headers I put:
Key = apiKey

Value = 1938adklw-2020etbw-3abca-9add0-433435

When I press Send in Postman, I get an access token from Postman, something like:


eyJhbGciOiJSUsdjklfklsdjlkfjioewsjfjsiovjioerj908834u89jf908wej98dsja98f4ejw984jf893jiadsguiods9f8j

Once I copy the token I go to another tab and create a GET request.

At Authorization I select the Type = Bearer Token and past the token above into it: eyJhbGciOiJSUsdjklfklsdjlkfjioewsjfjsiovjioerj908834u89jf908wej98dsja98f4ejw984jf893jiadsguiods9f8j

The URL is a GET request and looks like: https://worksauce.matrix2.de/list/clients
 
Does someone know how to translate this to Power Query?

Many thanks in advance!
1 ACCEPTED SOLUTION
Gjakova
Post Patron
Post Patron

Hi all, just to give an update, I managed to get it to work with the following script:

 

let
  GetJson = Web.Contents(
                       "https://company.abcde.com/",
                         [
                           RelativePath=tokenURL,
                           Headers = [#"Accept"="application/json", #"Content-Type"="application/json"],
           Content = Text.ToBinary("token=xxxxxxxx&iat=xxxxx*&resource=xxxxx&grant_type=client_credentials")
       ]
   ),
  FormatAsJson = Json.Document(GetJson),
  token = FormatAsJson[token],
  // add token in the URL:
  Source = Json.Document(
      Web.Contents(
        "https://company.abcde.com/", 
            [
          RelativePath=ListURL,
          Headers=[Authorization="Bearer "&token]
            ]
          )
        ),
  Navigation = Source[items],
  #"Converted to table" = Table.FromList(Navigation, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
  #"Converted to table"

The thing betweens " in the Content = part is not really important according to another topic of mine: https://community.powerbi.com/t5/Power-Query/Refreshable-token-API-Credential-Errors-when-trying-to-...

View solution in original post

2 REPLIES 2
Gjakova
Post Patron
Post Patron

Hi all, just to give an update, I managed to get it to work with the following script:

 

let
  GetJson = Web.Contents(
                       "https://company.abcde.com/",
                         [
                           RelativePath=tokenURL,
                           Headers = [#"Accept"="application/json", #"Content-Type"="application/json"],
           Content = Text.ToBinary("token=xxxxxxxx&iat=xxxxx*&resource=xxxxx&grant_type=client_credentials")
       ]
   ),
  FormatAsJson = Json.Document(GetJson),
  token = FormatAsJson[token],
  // add token in the URL:
  Source = Json.Document(
      Web.Contents(
        "https://company.abcde.com/", 
            [
          RelativePath=ListURL,
          Headers=[Authorization="Bearer "&token]
            ]
          )
        ),
  Navigation = Source[items],
  #"Converted to table" = Table.FromList(Navigation, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
  #"Converted to table"

The thing betweens " in the Content = part is not really important according to another topic of mine: https://community.powerbi.com/t5/Power-Query/Refreshable-token-API-Credential-Errors-when-trying-to-...

negi007
Community Champion
Community Champion

@Gjakova similar solution is available at below link. i also used the same for my data connection

 

https://community.powerbi.com/t5/Power-Query/Pull-data-from-a-REST-API-Authentication/td-p/246006

 




Did I answer your question? Mark my post as a solution!
Appreciate your Kudos



Proud to be a Super User!


Follow me on linkedin

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.