Forum Discussion
API with dynamic access token can't refresh in Power BI service
- 2 years ago
I realize this is an old thread and I'm not addressing the original question. But I finally found a solution to the expiring access token problem so I'm hoping I can help someone else.
In a Power BI dataflow, I'm calling a REST API that uses a Bearer token for authentication. The token expires in 2 hours. My dataflow refresh would run fine for 2 hours, and then fail with bad credentials. I was trying to call for a fresh access token every time, but it didn't work -- Power BI always seemed to use the original token, because it failed after exactly 2 hours.
Now it all finally makes sense to me after reading this excellent post: https://blog.crossjoin.co.uk/2017/01/06/web-contents-caching-and-the-excludedfromcachekey-option-in-power-bi-and-power-query/. It explains that "When you’re using the Web.Contents() M function to call a web service from Power Query or Power BI, you don’t necessarily get one HTTP request each time you call the function: some caching takes place, so that if you make the same request multiple times your query won’t waste time asking for the same data over and over."
So Power BI was trying to be helpful and caching the result of my first token call and reusing it the whole time. I thought that sending the same request over and over would give me new tokens, but Power BI said "oh, I see that you already made that request, and I already have the result, so here you go" ... OLD TOKEN!
Luckily, the fix is SO easy. Just send another made-up header into your token request so Power BI thinks you're asking for something different. For me, I sent in the hour of the day, so I knew that Power BI would go get me a new token every hour. It works!
Here's a snippet showing what I changed. I just made up a header called "VariableHeader" (you could name it whatever you want, the API ignores it), and gave it the hour of the day.
Hi Anonymous ,
I tried the solution you provided from the links, but did not work for my scenario. I need to access to a API with a dynamic access token which need to be refreshed every one hour. The access token is part of the header.
if I put the authorization part in a query, the authentication did not work.
it works using header, but it won't allow me to refresh in Power Bi service using this way.
Hi Anonymous
I have the same problem. Is there any solution for that?
Thanks