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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
PawelSow
Frequent Visitor

Refresh the authorization header

I created the custom data connector. The connector use a token authorization added to a request header. The connector looks like:

 

    let
        baseUrl = "[SOURCE_URL]",
        apiKey = Extension.CurrentCredential()[Username],
        apiSecret = Extension.CurrentCredential()[Password],
        token = MPODataConnector.TokenFunction(baseUrl, apiKey, apiSecret),
        source = OData.Feed(baseUrl, null, [Headers = [Authorization = token], MoreColumns=true])
    in
        source;

 

 This works fine but the problem is the generated token is valid only for 10min. If single query take longer that, then OData Api returns 'UnAuthoroized' response. Is there any way to force powerBi to regenrate the token after given amount of time.

3 REPLIES 3
Mahesh0016
Super User
Super User

@PawelSow  i hope this helps you!Thank You!!

let
baseUrl = "[SOURCE_URL]",
apiKey = Extension.CurrentCredential()[Username],
apiSecret = Extension.CurrentCredential()[Password],
GenerateToken = () => MPODataConnector.TokenFunction(baseUrl, apiKey, apiSecret),
token = GenerateToken(),
source = OData.Feed(baseUrl, null, [Headers = [Authorization = token], MoreColumns=true])
in
source

In this code, a function named GenerateToken is defined, which generates the token using the provided apiKey and apiSecret for the given baseUrl. This function can then be called right before making the OData request, ensuring that the token is always up-to-date.

However, please note that this approach might not be ideal for scenarios where multiple queries are being executed concurrently or in rapid succession. In such cases, you might consider using a more centralized token management approach, such as storing the token in a secure location and retrieving it when needed.

Another option would be to use a scheduled Power BI data refresh. You could schedule the refresh of your Power BI report or dataset at a frequency shorter than the token expiration time. This way, Power BI would regenerate the token during each refresh, ensuring that the data is always fetched with a valid token.

If the above approaches don't meet your requirements, you might need to explore custom solutions using Power Automate, Azure Logic Apps, or other automation tools that can trigger token regeneration and update your Power BI data source connection details as needed.

 

Hi

Thanks for the answer. however this seems to not work. The problem is that we have one single and long operation. The operation  load data of huge table which takes longer than default token expiration.   

This started to works when I add timeout property for OData connection. Many thanks for the Help

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.