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,
Thanks for the reply.
I have looked through the example peoject code and found a method:
function getToken() {
var dataret = '';
$.ajax({
type: "GET",
url: "/embedinfo/getembedinfo",
success: function (data) {
dataret = data;
var JSONdata = JSON.parse(data);
console.log(JSONdata);
var EmbedToken = JSONdata.EmbedToken;
var Expiration = EmbedToken.Expiration;
console.log('getToken - Expiration');
console.log(Expiration);
console.log('getToken - Expiration end');
console.log('getToken - report');
console.log(report);
lastLoadRequest = report.lastLoadRequest
console.log('getToken - report end');
console.log('getLastLoadDateTime - report start');
getLastLoadDateTime();
console.log('getLastLoadDateTime - report end');
},
error: function (err) {
console.log(dataret)
}
});
}setInterval(getToken, 15000);
public string GetEmbedInfo()
{
try
{
// Validate whether all the required configurations are provided in appsettings.json
string configValidationResult = ConfigValidatorService.ValidateConfig(azureAd, powerBI);
if (configValidationResult != null)
{
HttpContext.Response.StatusCode = 400;
return configValidationResult;
}
EmbedParams embedParams = pbiEmbedService.GetEmbedParams(new Guid(powerBI.Value.WorkspaceId), new Guid(powerBI.Value.ReportId));
return JsonSerializer.Serialize<EmbedParams>(embedParams);
}
catch (Exception ex)
{
HttpContext.Response.StatusCode = 500;
return ex.Message + "\n\n" + ex.StackTrace;
}
}This shows the token expiry in the console as increasing, but this doesn't work because if I wait until the original expiry date/time the report expires.
Is there a method of applying the new token to the existing report in the Web UI rather than reloading the page?
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 minutes
This 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.
- PowerBIUserHoop1 year agoFrequent Visitor
I tested this lat night and have had the site running today too, seems to be working well so far.
Thanks for the advice! 👍