Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!
Hi All,
I am generating embed token if expired using javascript using below is complete code:
var url = "https://app.powerbi.com/groups/5036f206-3d0e-46b8-8e3e-a54a8b35dd19/reports/447a6395-0ab8-46aa-9afa-aa67e11633eb/ReportSection9fdfd7dd0814177238c5"
var embedurl = "https://app.powerbi.com/reportEmbed?reportId=b4fc3b95-a1c4-462c-8ce4-3d6c0de928af&autoAuth=true&groupId=02a09d2e-96ea-480a-a402-a68d42f51e49";
var urlParams = url.split('/');
var groupId = urlParams[4];
var reportID = urlParams[6];
console.log(groupId,"&&", reportID);
$(document).ready(function () {
embedReportAndSetTokenListener(false, reportID, groupId, this.datasetId, "Create", "https://api.powerbi.com/beta/myorg/", embedurl);
});
function generateEmbedToken(reportID, groupID) {
var apiUrl = "https://api.powerbi.com/v1.0/myorg/groups/" + groupID + "/reports/" + reportID + "/GenerateToken"
var bearerToken = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkhsQzBSMTJza3hOWjFXUXdtak9GXzZ0X3RERSIsImtpZCI6IkhsQzBSMTJza3hOWjFXUXdtak9GXzZ0X3RERSJ9.eyJhdWQiOiJodHRwczovL2FuYWx5c2lzLndpbmRvd3MubmV0L3Bvd2VyYmkvYXBpIiwiaXNzIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvYzQyNjExM2UtZDNjZS00YWRiLWJiOTEtYzUzYjM3YzU4ZjE0LyIsImlhdCI6MTU4MDQ2NTQwNywibmJmIjoxNTgwNDY1NDA3LCJleHAiOjE1ODA0NjkzMDcsImFjY3QiOjAsImFjciI6IjEiLCJhaW8iOiI0Mk5nWU5pemJ5MmpnM2pSMWRmdVd1NFhjM0o3TjBXMzcvajZPU01xdkdwYVdGbmxjMGtBIiwiYW1yIjpbInB3ZCJdLCJhc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: apiUrl,
headers: {
"Content-Type": "application/json",
"Authorization": 'Bearer ${this.bearerToken}'
},
body: {
"accessLevel": "View",
"datasetId": this.datasetId
},
success: function (response) {
console.log('Token accessed');
},
failure: function(xhr, status, error){
console.log(error);
},
error: function (response) {
console.log(response);
}
});
return false;
}
function embedReportAndSetTokenListener(setAccessToken, reportId, groupId, datasetId, accessLevel, baseUri, embedUrl){
// Generate embed token
generateEmbedToken(reportId, groupId).then(function(Token) {
var embedToken = Token.token;
// set config for embedding report
var config = createConfig(embedToken,embedUrl,reportId);
// Get a reference to the embedded report HTML element
var embedContainer = $('#embedContainer')[0];
// Embed the report and display it within the div container.
var report = powerbi.embed(embedContainer, config);
// Report.off removes a given event handler if it exists.
report.off("loaded");
// Report.on will add an event handler which prints to Log window.
report.on("loaded", function() {
// Set token expiration listener
setTokenExpirationListener(Token.expiration, 2 /*minutes before expiration*/, reportId, groupId);
});
});
}
function setTokenExpirationListener(tokenExpiration,
minutesToRefresh,
reportId,
groupId){
// get current time
var currentTime = Date.now();
var expiration = Date.parse(tokenExpiration);
var safetyInterval = minutesToRefresh* 60 * 1000;
// time until token refresh in milliseconds
var timeout = expiration - currentTime - safetyInterval;
// if token already expired, generate new token and set the access token
if (timeout<=0)
{
console.log("Updating Report Embed Token");
updateToken(reportId, groupId);
}
// set timeout so minutesToRefresh minutes before token expires, token will be updated
else
{
console.log("Report Embed Token will be updated in " + timeout + " milliseconds.");
setTimeout(function() {
updateToken(reportId, groupId);
}, timeout);
}
}
function updateToken(reportId, groupId) {
// Generate new EmbedToken
generateEmbedToken(reportId, groupId)
.then(function( Token ) {
// Get a reference to the embedded report HTML element
var embedContainer = $('#embedContainer')[0];
// Get a reference to the embedded report.
var report = powerbi.get(embedContainer);
// Set AccessToken
report.setAccessToken(Token.token)
.then(function() {
// Set token expiration listener
// result.expiration is in ISO format
setTokenExpirationListener(Token.expiration,2 /*minutes before expiration*/);
});
});
} When i run application getting error like ypeError: generateEmbedToken(...).then is not a function
at embedReportAndSetTokenListener.
How could i fix this issue, Please suggest.
First of all, you're passing in the "Bearer " string to the authorisation header twice, so I don't think your token refresh would work. Also you probably don't need to scope it with `this.`.
var bearerToken = "Bearer ..."
...
"Authorization": 'Bearer ${this.bearerToken}'
The second part is your generateEmbedToken is expected to return a Promise, but all it does is execute an ajax call, so the error is saying that there is no `.then()` function on a boolean.
Hope this helps.
Were you able to find any solution.
If yes please post here, I am looking for the same thing.
Vote for your favorite vizzies from the Power BI World Championship submissions!
If you love stickers, then you will definitely want to check out our Community Sticker Challenge!
Check out the January 2026 Power BI update to learn about new features.
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 |