<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Auto Generate Embed Token using Javascript and PHP in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Auto-Generate-Embed-Token-using-Javascript-and-PHP/m-p/1317620#M25404</link>
    <description>&lt;P&gt;I can't be the only one that has run into this.&lt;/P&gt;&lt;P&gt;I have seen the ones for creating a function but I can't seem to get that to work either.&lt;/P&gt;&lt;P&gt;Has anyone gotten this to work with PHP and javascript?&lt;/P&gt;&lt;P&gt;Maybe a different authentication method would be better? Not sure what other options there might be.&lt;/P&gt;</description>
    <pubDate>Sun, 23 Aug 2020 21:07:55 GMT</pubDate>
    <dc:creator>Noobtastic_com</dc:creator>
    <dc:date>2020-08-23T21:07:55Z</dc:date>
    <item>
      <title>Auto Generate Embed Token using Javascript and PHP</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Auto-Generate-Embed-Token-using-Javascript-and-PHP/m-p/1316556#M25399</link>
      <description>&lt;P&gt;I have gotten my report working in test with a token generated using the Microsoft Embed Token - Generate Token (here&amp;nbsp;&lt;A href="https://docs.microsoft.com/en-us/rest/api/power-bi/embedtoken/generatetoken" target="_blank" rel="noopener"&gt;https://docs.microsoft.com/en-us/rest/api/power-bi/embedtoken/generatetoken&lt;/A&gt;) and by using the PowerShell commands.&lt;/P&gt;&lt;P&gt;I got the formatting just right, changed some config and got it all working just how I wanted it on localhost.&lt;/P&gt;&lt;P&gt;To try figuring it out, I also used the automated Embedded setup via powerbi.com (here&amp;nbsp;&lt;A href="https://app.powerbi.com/embedsetup/appownsdata" target="_blank" rel="noopener"&gt;https://app.powerbi.com/embedsetup/appownsdata)&lt;/A&gt;. I played around with the downloaded vs files which shows that generating the token on the fly is possible but it is all ASP.net and C# and I can't figure out how to convert it.&lt;/P&gt;&lt;P&gt;Now I am trying to get it deployed to production into my site which uses PHP and javascript.&lt;/P&gt;&lt;P&gt;Does anyone have some samples or anything where I could swap out my ReportId, GroupId, etc? Script kiddie style...&lt;/P&gt;&lt;P&gt;Here is what I am using that works perfectly, except for the expiring manually generated token:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;&amp;lt;script src="./dist/powerbi.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;div id="reportContainer" style="height: 1400px; width: 1000px;"&amp;gt;&amp;lt;/div&amp;gt;

  &amp;lt;script&amp;gt;
      // Get models. models contains enums that can be used.
      var models = window['powerbi-client'].models;

      // Embed configuration used to describe what and how to embed.
      // This object is used when calling powerbi.embed.
      // This also includes settings and options such as filters.
      // You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
      var embedConfiguration = {
          type: 'report',
          tokenType: models.TokenType.Embed,
          accessToken: '&amp;lt;manually generated token here&amp;gt;',
          embedUrl: 'https://app.powerbi.com/reportEmbed',
          id: '&amp;lt;report id here&amp;gt;',
          permissions: models.Permissions.Read,
          settings: {
              // filterPaneEnabled: false,
              // navContentPaneEnabled: true,
              background: models.BackgroundType.Transparent,
              panes:{
                bookmarks: {
                  visible: false
                },
                fields: {
                  expanded: false
                },
                filters: {
                  expanded: false,
                  visible: false
                },
                pageNavigation: {
                  visible: true
                },
                selection: {
                  visible: false
                },
                syncSlicers: {
                  visible: false
                },
                visualizations: {
                  expanded: false
                }
              }

          }
      };

      // Get a reference to the embedded report HTML element
      var $reportContainer = $('#reportContainer')[0];

      // Embed the report and display it within the div container.
      var report = powerbi.embed($reportContainer, embedConfiguration);
  &amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Aug 2020 20:36:09 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Auto-Generate-Embed-Token-using-Javascript-and-PHP/m-p/1316556#M25399</guid>
      <dc:creator>Noobtastic_com</dc:creator>
      <dc:date>2020-08-22T20:36:09Z</dc:date>
    </item>
    <item>
      <title>Re: Auto Generate Embed Token using Javascript and PHP</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Auto-Generate-Embed-Token-using-Javascript-and-PHP/m-p/1317620#M25404</link>
      <description>&lt;P&gt;I can't be the only one that has run into this.&lt;/P&gt;&lt;P&gt;I have seen the ones for creating a function but I can't seem to get that to work either.&lt;/P&gt;&lt;P&gt;Has anyone gotten this to work with PHP and javascript?&lt;/P&gt;&lt;P&gt;Maybe a different authentication method would be better? Not sure what other options there might be.&lt;/P&gt;</description>
      <pubDate>Sun, 23 Aug 2020 21:07:55 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Auto-Generate-Embed-Token-using-Javascript-and-PHP/m-p/1317620#M25404</guid>
      <dc:creator>Noobtastic_com</dc:creator>
      <dc:date>2020-08-23T21:07:55Z</dc:date>
    </item>
    <item>
      <title>Re: Auto Generate Embed Token using Javascript and PHP</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Auto-Generate-Embed-Token-using-Javascript-and-PHP/m-p/1325065#M25449</link>
      <description>&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/216775"&gt;@Noobtastic_com&lt;/a&gt;,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;AFAIK, power bi generates token API include token string, expired time. I think you need to do add some additional processes to store and check generated tokens and the expired time.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;For example: &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Add if statements to check the expired time and stored token codes into your script. &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;If tokens expired or did not exist, you can invoke power bi rest to generated new tokens and corresponding token lifecycles and stored to sessions.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Regards,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Xiaoxin Sheng&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Aug 2020 08:30:42 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Auto-Generate-Embed-Token-using-Javascript-and-PHP/m-p/1325065#M25449</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-08-26T08:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: Auto Generate Embed Token using Javascript and PHP</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Auto-Generate-Embed-Token-using-Javascript-and-PHP/m-p/1333656#M25499</link>
      <description>&lt;P&gt;I got the answer over on Stack Overflow:&amp;nbsp;&lt;A href="https://stackoverflow.com/questions/63564271/how-to-auto-generate-embed-token-using-javascript-and-php/63649863#63649863" target="_blank"&gt;https://stackoverflow.com/questions/63564271/how-to-auto-generate-embed-token-using-javascript-and-php/63649863#63649863&lt;/A&gt;&lt;/P&gt;&lt;P&gt;For anyone else, below is the working code with the formatting that I used. It should be mostly plug and play after changing 5 values to those applicable to your report.&lt;/P&gt;&lt;P&gt;Note that I couldn't get it to work on localhost due errors that appeared to be related to an authentication issue between localhost and the PowerBI servers. It does work on the live site.&lt;/P&gt;&lt;P&gt;Feel free to make it your own. Hopefully it will work with very minimal changes required.&lt;/P&gt;&lt;LI-CODE lang="php"&gt;&amp;lt;script src="https://code.jquery.com/jquery-1.11.3.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="./dist/powerbi.js"&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;div id="reportContainer" style="height: 1400px; width: 1000px;"&amp;gt;&amp;lt;/div&amp;gt;

&amp;lt;?php
    // All the values used below can be generated at https://app.powerbi.com/embedsetup/appownsdata
    
    /* Get oauth2 token using a POST request */
    $curlPostToken = curl_init();
    curl_setopt_array($curlPostToken, array(
        CURLOPT_URL =&amp;gt; "https://login.windows.net/common/oauth2/token",
        CURLOPT_RETURNTRANSFER =&amp;gt; true,
        CURLOPT_ENCODING =&amp;gt; "",
        CURLOPT_MAXREDIRS =&amp;gt; 10,
        CURLOPT_TIMEOUT =&amp;gt; 30,
        CURLOPT_HTTP_VERSION =&amp;gt; CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST =&amp;gt; "POST",
        CURLOPT_POSTFIELDS =&amp;gt; array(
            grant_type =&amp;gt; 'password',
            scope =&amp;gt; 'openid',
            resource =&amp;gt; 'https://analysis.windows.net/powerbi/api',
            
            // Make changes Start
            client_id =&amp;gt; '#####################', // Registered App Application ID
            username =&amp;gt; 'john.doe@yourdomain.com', // for example john.doe@yourdomain.com
            password =&amp;gt; '#####################', // Azure password for above user
            // Make changes End
        )
    ));

    $tokenResponse = curl_exec($curlPostToken);
    $tokenError = curl_error($curlPostToken);
    curl_close($curlPostToken);

    // decode result, and store the access_token in $embeddedToken variable:
    $tokenResult = json_decode($tokenResponse, true);
    $token = $tokenResult["access_token"];
    $embeddedToken = "Bearer "  . ' ' .  $token;

    /* Use the token to get an embedded URL using a GET request */
    $curlGetUrl = curl_init();
    curl_setopt_array($curlGetUrl, array(
        
        // Make changes Start
        CURLOPT_URL =&amp;gt; "https://api.powerbi.com/v1.0/myorg/groups/#####################/reports/", // Enter your Workspace ID, aka Group ID
        // Make changes End
        
        CURLOPT_RETURNTRANSFER =&amp;gt; true,
        CURLOPT_ENCODING =&amp;gt; "",
        CURLOPT_MAXREDIRS =&amp;gt; 10,
        CURLOPT_TIMEOUT =&amp;gt; 30,
        CURLOPT_HTTP_VERSION =&amp;gt; CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST =&amp;gt; "GET",
        CURLOPT_HTTPHEADER =&amp;gt; array(
            "Authorization: $embeddedToken",
            "Cache-Control: no-cache",
        ),
    ));

    $embedResponse = curl_exec($curlGetUrl);
    $embedError = curl_error($curlGetUrl);
    curl_close($$curlGetUrl);
    if ($embedError) {
        echo "cURL Error #:" . $embedError;
        } else {
            $embedResponse = json_decode($embedResponse, true);
            $embedUrl = $embedResponse['value'][0]['embedUrl']; // this is just taking the first value. you need logic to find the report you actually want to embed. This EmbedUrl needs to match the corresponding ReportId you later use in the JavaScript.
        }
?&amp;gt;



&amp;lt;script&amp;gt;
    // Get models. models contains enums that can be used.
    var models = window['powerbi-client'].models;

    // Embed configuration used to describe the what and how to embed.
    // This object is used when calling powerbi.embed.
    // This also includes settings and options such as filters.
    // You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.

    var embedConfiguration= {
        type: 'report',
        
        // Make changes Start
        id: '#####################', // the report ID
        // Make changes End
        
        embedUrl: "&amp;lt;?php echo $embedUrl ?&amp;gt;",
        accessToken: "&amp;lt;?php echo $token; ?&amp;gt;",
        permissions: models.Permissions.Read,
        settings: {
            background: models.BackgroundType.Transparent,
            panes:{
                bookmarks: {
                    visible: false
                },
                fields: {
                    expanded: false
                },
                filters: {
                    expanded: false,
                    visible: false
                },
                pageNavigation: {
                    visible: false
                },
                selection: {
                    visible: false
                },
                syncSlicers: {
                    visible: false
                },
                visualizations: {
                    expanded: false
                }
            }
        }
    };

    var $reportContainer = $('#reportContainer');
    var report = powerbi.embed($reportContainer.get(0), embedConfiguration);

&amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Aug 2020 17:33:04 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Auto-Generate-Embed-Token-using-Javascript-and-PHP/m-p/1333656#M25499</guid>
      <dc:creator>Noobtastic_com</dc:creator>
      <dc:date>2020-08-29T17:33:04Z</dc:date>
    </item>
  </channel>
</rss>

