How do I get token using javascript API while trying to embed graphs using Power BI

0 votes
I have a few graphs in Power BI that i need to share it with my clients. I thought of making a custom page on the server and embedding the graphs on this server using Power BI Embedded setup.

I think i need an access token via javascript API to do the following. but i donnot know how to get this access token.

Thank you..
Oct 3, 2018 in Power BI by Hannah
• 18,520 points
4,587 views

3 answers to this question.

0 votes
To get this access token you first need to generate it. Generate EmbededToken is actually a REST API call. NodeJS or AJAX to issue this request.
answered Oct 3, 2018 by Nilesh
• 7,060 points
0 votes

You need to use the generateEmbededToken function. It calls the application Back End and returns the Embed Token.

The function would look something like this:

function embedReportAndSetTokenListener(setAccessToken = false, 
    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 = 2, 
    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*/);
        });
    });
}
answered Oct 3, 2018 by Kalgi
• 52,350 points
0 votes
Use ADAL.js that will give you an access token
answered Jun 22, 2019 by anonymous

Related Questions In Power BI

0 votes
0 answers
0 votes
1 answer

How do you embed dashboard using JavaScript in Power BI?

Dashboards in Power BI can be embedded ...READ MORE

answered Mar 5, 2019 in Power BI by Phalguni
• 1,020 points
2,065 views
0 votes
1 answer

I have a web API with basic authorisation and username and password. How do i connect to it in Power BI?

Hey, @There, I found your query is quite ...READ MORE

answered Oct 29, 2020 in Power BI by Gitika
• 65,770 points
3,627 views
0 votes
1 answer

As a beginner, which bi tool do i need to learn to get job (tableau or power bi)?

Hey, If your focus is to get a ...READ MORE

answered Mar 16, 2020 in Power BI by Gitika
• 65,770 points
1,188 views
0 votes
1 answer

Displaying Table Schema using Power BI with Azure IoT Hub

Answering your first question, Event Hubs are ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,350 views
+1 vote
1 answer

Unable to install connector for Power Bi and PostgreSQL

I think the problem is not at ...READ MORE

answered Aug 22, 2018 in Power BI by nirvana
• 3,130 points
2,748 views
+2 votes
2 answers

Migrate power bi collection to power bi embedded

I agree with Kalgi, this method is ...READ MORE

answered Oct 11, 2018 in Power BI by Hannah
• 18,520 points
1,521 views
+1 vote
1 answer

Connect power bi desktop to dataset and create custom reports

Yes using Power BI REST API to ...READ MORE

answered Sep 18, 2018 in Power BI by Kalgi
• 52,350 points
1,670 views
+2 votes
2 answers

Power BI service throwing 403 error when using token to embed Power BI report

To add to @Kalgi's answer, Also make ...READ MORE

answered Oct 3, 2018 in Power BI by Nilesh
• 7,060 points
6,244 views
+1 vote
2 answers

Passing parameters to Power BI filter programmatically

In addition to @Kalgi's answer, there's another ...READ MORE

answered Sep 19, 2018 in Power BI by Nilesh
• 7,060 points
8,372 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP