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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
etsygankov
Frequent Visitor

Automatic refresh of Access Token doesn't work

I am "emedding for organization" and trying to use the automatic refresh of access token described here by providing eventHooks.accessTokenProvider.

 

Here is the code that does embedding (notice accessTokenProvider and getNewToken implementation):

 

 

 

let getNewToken = async function () {
            
    var result = await $.ajax({
        type: "POST",
        url: "/gettoken"
    });

    return result.Token;
}

let config = {
    type: type,
    tokenType: models.TokenType.Aad,
    accessToken: common.accessToken,
    embedUrl: embedUrl,
    id: embedId,
    settings: settings,
    eventHooks: {
        accessTokenProvider: getNewToken
    }
};

 

 

 

Report loads fine and then I see periodic calls to /powerbi/refresh/subscribe in Fiddler. They complete with HTTP 200.

 

After some time, the access token expires, and I can see that PowerBI calls my getNewToken function and that the new refresh token is being returned -- the Chrome debugger shows the updated access token on line "return result.Token". 

 

But subsequent calls to /powerbi/refresh/subscribe still have the old access token in Authorization: Bearer and fail with HTTP 401.

 

Does anybody know why the updated access token returned from accessTokenProvider is not being used for further ajax calls to PowerBI?

 

Do you have any working examples of implementing accessTokenProvider?

 

Thank you!

 

 

3 REPLIES 3
Anonymous
Not applicable

Hi @etsygankov,

I'm not so sure if the issue is caused by the compatibility of the ajax function and accessTokenProvider, have you tried to use other types of functions instead?

In addition, you can also take a look at the following document if it helps:

Refresh the access token in Power BI embedded analytics | Microsoft Docs

Regards,

Xiaoxin Sheng

The following example from documentation is what I used as a template for writing my function:

 

etsygankov_0-1652358338684.png

Do you know if "return token" is a string or a type? If a type, is there documentation on what that type should be?

 

Thanks

Anonymous
Not applicable

HI @etsygankov,

The token should be a variable defined with string type in the function and used to get the token string from API response.
Regards,

Xiaoxin Sheng

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

May 2025 Monthly Update

Fabric Community Update - May 2025

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