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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
feedme
New Member

Authentication issues when calling multiple REST APIs with dynamic access token

Hi all, 

 

I am currently trying to call mutliple tables from an API provider in an Oauth authentication flow with a bearer token. The calls work fine individually but as soon as I want to call more than one table in Power Query Editor I get notified about authentication issues

 

mfidev_0-1684406790116.png

 

I retrieve the token dynamically with the function fnGetToken. 

 

() =>
let

   //Send a POST request to login and get token
   loginData = "{""client_id"": """ & xxxxxxxxxxx & """, ""client_secret"": """ & xxxxxxxx & "}",
   loginHeaders = [
      #"Content-Type" = "application/json",
      #"accept" = "application/json",
      #"login_id" = "xxxxxxxxx"
   ],

   loginResponse = Json.Document(Web.Contents(
      base_url,
      [
      RelativePath = "v1/authenticate",
      Headers = loginHeaders,
      Content = Text.ToBinary(loginData)]
   )),
   data = loginResponse[data],
   token = "Bearer " & data[token]
in
   token

 

I have tried to call the fnGetToken function before the call and load the value on a variable as well as in the request itself. 

I have also used the Function.InvokeFunction with a 15 seconds delay using the #duration Parameter.

 

let
   accesstoken = fnGetToken(),
   getData =
      Json.Document(
         Web.Contents(
            xxxxxbase_urlxxxxxx,
            [
               RelativePath = xxxxxrelative_urlxxxxxx,
               Headers=[
                  #"login_id"="xxxxxxxxxx",
                  accept="application/json",
                  #"authorization" = accesstoken
               ]
            ]
         )
      ),
   data = getData[data]
in
   data

 

My last approach would be to put each of them in individual Dataflows but would love to hear if there is a proper way with M code directly. Very much appreciate your help 🙂

 

2 REPLIES 2
cpwebb
Microsoft Employee
Microsoft Employee

Are you building a custom connector here?

 

If you want to a REST API that needs OAuth2 authentication, you should build a custom connector (despite what you might have read on various blog posts). See https://blog.crossjoin.co.uk/2021/08/29/connecting-to-rest-apis-with-oauth2-authentication-in-power-... for more details.

Thanks for your response.

I saw your blog-posts about that topic but since I managed to establish functioning REST API calls I had the hope I have magic power and could do it again 😅   

I guess I will dive into developing custom connectors which I remember you saying in a youtube video is also a very complex topic. So lets see how that goes. 

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Kudoed Authors