Forum Discussion

PowerBIUserHoop's avatar
PowerBIUserHoop
Frequent Visitor
1 year ago
Solved

Refreshing the Report Token

Hi,   I am new to developing in PowerBI, I am looking at creating a dashbosrd from a PowerBI report using ASP.Net Core.   I have the report running fine, but am struggling with how to refresh the...
  • FarhanJeelani's avatar
    FarhanJeelani
    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 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.