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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

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
Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.