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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
MichaelvdZ
New Member

How to define a variable for dynamic refresh token for api calls

Hi guys,

 

I try to use a Rest API that uses an access token as authentication, which expires in 10 minutes. When this access token is expired, I need to get a new access token using a refresh token, that can be used once and is valid for 30 days. Each time you request a new access token, a new refresh token is returned aswell that must be used in the next refresh. Therefore, the refresh token needs to be stored somewhere, where it can both be (1) updated in the current refresh run and (2) collected to refresh the access token in the next refresh run. This is were we run into the problem as we do not know how to setup a framework in Power BI that facilitates both these options.

 

Each time we request a new access token, the response is as follows:

{

   "access_token": "xxxxxxx",

   "token_type": "bearer",

   "expires_in": "600",

   "refresh_token": "yyyyyy"

}

 

Storing both tokens in a table, seems to be the logical option here, but there is an additonal catch; it can occur that the refresh of the data takes more than 10 minutes. In which case the access_token needs to be refreshed multiple times during the same Power BI refresh run. And since the refresh_token also changes after refreshing the access_token, it cannot be stored in a parameter. 

 

The issue is almost similar to the following issue: Power-Query/Channel-Advisor-API-Connection-with-auth-key-creation

 

However our refresh_token (authentication) is not static (like a username and password), but changes after each refresh.

 

Is there any way to define a variable for the refresh_token that can be used in each different query and that can also be updated whenever you request a new access_token (even during the same refresh run)?

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Power BI has no memory and no concept of (mutable) variables. You need to treat each API call as if it were the first.

View solution in original post

3 REPLIES 3
peggysue
Advocate I
Advocate I

Hi @MichaelvdZ, instead of trying to refresh my access token, I found a really easy way to have Power BI request a new access token after my original one expired. I hope this helps!

 

See my post in this thread: https://community.fabric.microsoft.com/t5/Service/API-with-dynamic-access-token-can-t-refresh-in-Pow...

 

☝️ this is simply awesome

 

@peggysue  can't thank you enough. The method works , I wasted a whole week on "AI" to find out how to refresh token as my jobs were running for an hour and getting errored out due to expired token.

 

You saved me from eventually being frustrated on this.

 

I have run several test jobs following this 1hr+, 2hr+ and it never errored out on an expired token.

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
lbendlin
Super User
Super User

Power BI has no memory and no concept of (mutable) variables. You need to treat each API call as if it were the first.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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