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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
bboy81905
Frequent Visitor

PowerBI-JavaScript setAccessToken doesn't work

I'm using PowerBI-JavaScript library to embed the dashboard on my website :

var config = {
        type: 'dashboard',
        tokenType: models.TokenType.Embed,
        accessToken: accessToken,
        embedUrl: embedUrl,
        id: embedDashboardId
    };

    // Get a reference to the embedded dashboard HTML element
    var dashboardContainer = $('#dashboardContainer')[0];

    // Embed the dashboard and display it within the div container.
    var dashboard = powerbi.embed(dashboardContainer, config);

I wanna use setAccessToken to update my AccessToken keeping dashboard not expired, so I make function below :

function setToken() {
       
        dashboard.setAccessToken("newtoken")
            .then(function (r) {
                console.log(r);
                console.log("Update success!!")
            })
            .catch(function (e) {
                console.error('Error setting access token', e);
            });

    }

I can see "Update success!!" on console eventually. But when I use getAccessToken(), AccessToken is same as before. It didn't update my AccessToken!!

Please give me a advise or solution to fix this problem! I would appreciate.

1 ACCEPTED SOLUTION
alexsilvar
Frequent Visitor

Hi bboy81905,

 

How you can see at the code in the lib, when you set the new access token it send it to the service, and aparently does not store your token:

 

 

Embed.prototype.setAccessToken = function (accessToken) {
	        var embedType = this.config.type;
	        return this.service.hpm.post('/' + embedType + '/token', accessToken, { uid: this.config.uniqueId }, this.iframe.contentWindow)
	            .then(function (response) {
	            return response.body;
	        })
	            .catch(function (response) {
	            throw response.body;
	        });
	    };

And when you call getAccessToken it will return the token wich you provided first in the config object (if it is lazy evaluated, as I think it is):

 

 

Embed.prototype.getAccessToken = function (globalAccessToken) {
	        var accessToken = this.config.accessToken || this.element.getAttribute(Embed.accessTokenAttribute) || globalAccessToken;
	        if (!accessToken) {
	            throw new Error("No access token was found for element. You must specify an access token directly on the element using attribute '" + Embed.accessTokenAttribute + "' or specify a global token at: powerbi.accessToken.");
	        }
	        return accessToken;
	    };

I think you need to change the code at the "setAccessToken" function and add a line like:

 

this.config.accessToken = accessToken;

It will probably solve your problem but i dont  think it is the right way to do.

 

View solution in original post

8 REPLIES 8
dp001
New Member

Have you got the solution work?

I have the same problem. The token expires in one hour.

Setting setAccessToken() doesn't work in my report.

Anonymous
Not applicable

@dp001 me too did you get it work ?

var report = null;

 

window.onload = function () {



var accessToken = document.getElementById('MainContent_accessToken').value;


var config = {
type: 'report',
accessToken: accessToken,
embedUrl: embedUrl,
id: reportId,
settings: {
filterPaneEnabled: false,
navContentPaneEnabled: true
}
};


var reportContainer = document.getElementById('reportContainer');


report = powerbi.embed(reportContainer, config);

 

setTimeout(SetNewToken, 3120000)
};

 

function refreshToken() {
var newToken = document.getElementById('MainContent_accessToken').value;
report.setAccessToken(newToken)
};

 

function SetNewToken() {
refreshToken();
setTimeout(SetNewToken, 3120000)
}

Yes, I got it work.

I get the new token every 55minutes in server and set it in a hidden field.

In client side, set the new token every 56 minutes
report.setAccessToken(newToken)

Hi, 

I am trying to refresh token after 55 mins.

I got the new token by using GenerateTokenAsync() method in c# and passed it to js file and I used setAccessToken(newToken) method there.
But my embedded report is not getting updated with the new token.
Can you please tell me how you got the new token from server side?

 

alexsilvar
Frequent Visitor

Hi bboy81905,

 

How you can see at the code in the lib, when you set the new access token it send it to the service, and aparently does not store your token:

 

 

Embed.prototype.setAccessToken = function (accessToken) {
	        var embedType = this.config.type;
	        return this.service.hpm.post('/' + embedType + '/token', accessToken, { uid: this.config.uniqueId }, this.iframe.contentWindow)
	            .then(function (response) {
	            return response.body;
	        })
	            .catch(function (response) {
	            throw response.body;
	        });
	    };

And when you call getAccessToken it will return the token wich you provided first in the config object (if it is lazy evaluated, as I think it is):

 

 

Embed.prototype.getAccessToken = function (globalAccessToken) {
	        var accessToken = this.config.accessToken || this.element.getAttribute(Embed.accessTokenAttribute) || globalAccessToken;
	        if (!accessToken) {
	            throw new Error("No access token was found for element. You must specify an access token directly on the element using attribute '" + Embed.accessTokenAttribute + "' or specify a global token at: powerbi.accessToken.");
	        }
	        return accessToken;
	    };

I think you need to change the code at the "setAccessToken" function and add a line like:

 

this.config.accessToken = accessToken;

It will probably solve your problem but i dont  think it is the right way to do.

 

Thank you for your answer !!  ^^

bboy81905
Frequent Visitor

I'm using PowerBI-JavaScript library to embed the dashboard on my website :

 var config = {
        type: 'dashboard',
        tokenType: models.TokenType.Embed,
        accessToken: accessToken,
        embedUrl: embedUrl,
        id: embedDashboardId
    };

    // Get a reference to the embedded dashboard HTML element
    var dashboardContainer = $('#dashboardContainer')[0];

    // Embed the dashboard and display it within the div container.
    var dashboard = powerbi.embed(dashboardContainer, config);

I wanna use setAccessToken to update my AccessToken keeping dashboard not expired, so I make function below :

    function setToken() {
       
        dashboard.setAccessToken("newtoken")
            .then(function (r) {
                console.log(r);
                console.log("Update success!!")
            })
            .catch(function (e) {
                console.error('Error setting access token', e);
            });

    }

I can see "Update success!!" on console eventually. But when I use getAccessToken(), AccessToken is same as before. It didn't update my AccessToken!!

Please give me a advisement or solution to fix this problem! I would appreciate 🙂

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.