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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
pradvisors
New Member

Power BI Service Refresh Fails For REST API Requiring Bearer Token

On Power BI Desktop I am connecting to a REST API that exposes company data requried for reporting.  The API expects a bearer token for authentication. I have used the following approach to generate the bearer token and then inject it into the API call to retrieve data.  This works fine on Power BI desktop but fails after publishing to Power BI Service.  While this works on Power BI desktop I am wondering if this is a limitation to Power BI Service and if this is the best approach.  

 

  1. Create a function called GetBearerToken includes the following logic:  

    () =>
    let
    clientId = "{your_client_id}",
    clientSecret = "{your_client-secret}",
    tokenUrl = "{token_server_url}",
    body = "client_id=" & clientId & "&client_secret=" & clientSecret & "&grant_type=client_credentials",
    headers = [#"Content-Type" = "application/x-www-form-urlencoded"],
    tokenResponse = Web.Contents(tokenUrl, [Headers=headers, Content=Text.ToBinary(body)]),
    tokenJson = Json.Document(tokenResponse),
    accessToken = tokenJson[access_token]
    in
    accessToken
  2. Inject the results of the GetBearerToken function into the request to get data from the REST API (M Query)
    let
    Source = Json.Document(Web.Contents("{rest_endpont_url}", [Headers=[Authorization="Bearer " & GetBearerToken()]])),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "name", "contact", "address1", "address2", "city", "state", "zipCode", "phone", "fax", "email", "searchCode"}, {"id", "name", "contact", "address1", "address2", "city", "state", "zipCode", "phone", "fax", "email", "searchCode"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1",{{"id", Int64.Type}, {"name", type text}, {"contact", type text}, {"address1", type text}, {"address2", type text}, {"city", type text}, {"state", type text}, {"zipCode", type text}, {"phone", type text}, {"fax", type text}, {"email", type text}, {"searchCode", type any}})
    in
    #"Changed Type"

I am using a FROM WEB data source with anonymous credentials since the bearer token is being injected into the M Query at runtime.  When I publish to Power BI Service and attempt to manually refresh the data or schedule a data refresh, I receive the following error.  

 

Scheduled refresh is disabled because at least one data source is missing credentials.

 

Is this a limitation of Power BI Service or is there a better approach for retrieving data from a REST API that requires authentication via JWT?  

 

2 REPLIES 2
pradvisors
New Member

I've taken the M code from the generic query and moved it into a separate report that only displays the bearer token result.  Refreshing the data in Power BI desktop works as expected with a new bearer token being displayed.  Publishing to Power BI Service and attempting to refresh the data returns an error and does not display a new bearer token.  Oddly, the error references a scheduled refresh but I am doing a manual refresh on the model.  "Scheduled refresh is disabled because at least one data source is missing credentials. To start the refresh again, go to this dataset's settings page and enter credentials for all data sources. Then reactivate scheduled refresh."  I am starting to think this is a use case that is not supported by Power BI Service.  Seems odd as this is a pretty standard way to surface corporate data without direct access to databases.  

GilbertQ
Super User
Super User

Hi @pradvisors 

 

What I would suggest doing is to put the function with your actual M code into a single table and see if that works?





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

Proud to be a Super User!







Power BI Blog

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.