How to auto generate embed token using javascript?

Tags: ,

I am using powerbi embedded. I successfully embed a report into my application using javascript. But I have to update embedded token manually every time.

Now I want to call rest API from my code so that token update automatically. How to call API from my code to generate token and how to update token when it’s going to expire?

My code is :

  <script src="/Scripts/powerbi.js"></script>

 <div id="captionArea">
    <h1>Power BI Embed test</h1>
  <div id="embedContainer" style="height:500px">
    (function () {
      // Please change these values
      var txtAccessToken = 'H4sIAAAAAA...';
      var txtEmbedUrl =
      var txtEmbedReportId = 'b21f4f90-e364-4b4c-9281-c5db87cdf3a5';
      var models = window['powerbi-client'].models;
      var permissions = models.Permissions.All;
      var config = {
        type: 'report',
        tokenType: models.TokenType.Embed,
        accessToken: txtAccessToken,
        embedUrl: txtEmbedUrl,
        id: txtEmbedReportId,
        permissions: permissions,
        settings: {
          filterPaneEnabled: true,
          navContentPaneEnabled: true
      var embedContainer = document.getElementById('embedContainer');
      var report = powerbi.embed(embedContainer, config);


I solved it. You can use MSAL or ADAL to generate an access token. I used MSAL.js 1.0 with implicit grant flow:

After getting access token you can use ajax to call REST-API to generate embed token.

You can follow this Failed to load response data when tried to get access token from azure using JavaScript

Source: stackoverflow