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,747 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
0 votes

Hello

You can follow below steps-

  1. Register an application in Azure Active Directory (Azure AD).
  2. Grant permissions to access Power BI resources.
  3. Obtain a client ID and client secret for your application.
  4. Use the client ID, client secret, and other parameters to request an access token using the OAuth 2.0 authorization flow.
  5. Once you have the access token, you can use it to authenticate and embed the Power BI graphs on your server.
Hope above steps will help you.
Thank you. 
answered Jul 19, 2023 by Bob

edited Mar 5
0 votes
You need to use the generate embed token function... It calls the application back end and returns the embed token...
answered Oct 10, 2023 by Monika kale

edited Mar 5

Related Questions In Power BI

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do you embed dashboard using JavaScript in Power BI?

Make sure the use the correct embedURL ...READ MORE

answered Oct 10, 2023 in Power BI by Monika kale

edited Mar 5 2,154 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,922 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,522 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,870 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,651 views
+1 vote
1 answer

Connect power bi desktop to dataset and create custom reports

Open power bi report nd sign in ...READ MORE

answered Oct 10, 2023 in Power BI by Monika kale

edited Mar 5 1,796 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,511 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,559 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