Forum Discussion
Refreshing the Report Token
- 1 year ago
To refresh the embed token in Power BI without reloading the page, you can replace the current token by calling the `setAccessToken` method on the report object in the Web UI. This method will apply the new token without requiring a page reload, keeping the session alive.
Here's how you could modify your `getToken` method to use `setAccessToken`:
1. Modify the `getToken` function: Update it to call `report.setAccessToken(newToken)` when a new token is fetched.
2. Implement `setAccessToken`: After parsing the new token from the response, apply it directly to the existing report instance.
Here's how your modified code could look:
javascript
function getToken() { $.ajax({ type: "GET", url: "/embedinfo/getembedinfo", success: function (data) { var JSONdata = JSON.parse(data); var newToken = JSONdata.EmbedToken.Token; var Expiration = JSONdata.EmbedToken.Expiration; // Update the existing report with the new token if (report) { report.setAccessToken(newToken) .then(() => { console.log('Token refreshed successfully'); }) .catch(error => { console.error('Error setting new token:', error); }); } console.log('New Token Expiration:', Expiration); }, error: function (err) { console.error('Error fetching token:', err); } }); } // Refresh token every 15 minutes or as required setInterval(getToken, 900000); // 15 minutesThis approach will ensure that the new token is seamlessly applied to the report without needing to reload the page, maintaining the user's session with minimal interruption. Adjust the interval to refresh the token before the expiration time.
Hi PowerBIUserHoop ,
As far as I know, access tokens generally expire in 60 minutes or less, and you'll need an access token policy and an update to it.
Refresh tokens in the Microsoft identity platform - Microsoft identity platform | Microsoft Learn
This is the related document, you can view this content:
oauth - Handling Expired Refresh Tokens in ASP.NET Core - Stack Overflow
Solved: Get data from API with token that expires - Microsoft Fabric Community
Solved: Token expiring in Power BI embedded (error 403) - Microsoft Fabric Community
Best Regards,
Liu Yang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.