<?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: Refresh embed token using javascript in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Refresh-embed-token-using-javascript/m-p/1995686#M30892</link>
    <description>&lt;P&gt;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.`.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var bearerToken = "Bearer ..."

...

     "Authorization": 'Bearer ${this.bearerToken}'&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Hope this helps.&lt;/P&gt;</description>
    <pubDate>Tue, 03 Aug 2021 15:01:44 GMT</pubDate>
    <dc:creator>AndreiPopa</dc:creator>
    <dc:date>2021-08-03T15:01:44Z</dc:date>
    <item>
      <title>Refresh embed token using javascript</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Refresh-embed-token-using-javascript/m-p/918066#M22177</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am generating embed token if expired using javascript using below is complete code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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&amp;amp;autoAuth=true&amp;amp;groupId=02a09d2e-96ea-480a-a402-a68d42f51e49";
        

        var urlParams = url.split('/');
        var groupId = urlParams[4];
       
        var reportID = urlParams[6];
        console.log(groupId,"&amp;amp;&amp;amp;", 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.eyJhdWQiOiJodHRwczovL2FuYWx5c2lzLndpbmRvd3MubmV0L3Bvd2VyYmkvYXBpIiwiaXNzIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvYzQyNjExM2UtZDNjZS00YWRiLWJiOTEtYzUzYjM3YzU4ZjE0LyIsImlhdCI6MTU4MDQ2NTQwNywibmJmIjoxNTgwNDY1NDA3LCJleHAiOjE1ODA0NjkzMDcsImFjY3QiOjAsImFjciI6IjEiLCJhaW8iOiI0Mk5nWU5pemJ5MmpnM2pSMWRmdVd1NFhjM0o3TjBXMzcvajZPU01xdkdwYVdGbmxjMGtBIiwiYW1yIjpbInB3ZCJdLCJhcHBpZCI6IjdmNTlhNzczLTJlYWYtNDI5Yy1hMDU5LTUwZmM1YmIyOGI0NCIsImFwcGlkYWNyIjoiMiIsImZhbWlseV9uYW1lIjoiUmVwb3J0cyIsImdpdmVuX25hbWUiOiJQSE0iLCJpcGFkZHIiOiIxMDYuMTk3LjE5OS4xMDMiLCJuYW1lIjoiUEhNIFJlcG9ydHMiLCJvaWQiOiJhMjBkNTZkMi05MjlkLTQ2NTctOTY2Ny05NjRhNzU0YTM0ODQiLCJvbnByZW1fc2lkIjoiUy0xLTUtMjEtMjA0MTg1ODIxNS0yODY0NjY5MTUwLTM2ODc0NzYyNS0xNjYyOSIsInB1aWQiOiIxMDAzN0ZGRUE5M0ZFOTI2Iiwic2NwIjoidXNlcl9pbXBlcnNvbmF0aW9uIiwic3ViIjoiY29mOFFBcFJWTEpVWnowZnozTHhvbUFVWmdqM2FMWWxINFdUbnFJMVdyayIsInRpZCI6ImM0MjYxMTNlLWQzY2UtNGFkYi1iYjkxLWM1M2IzN2M1OGYxNCIsInVuaXF1ZV9uYW1lIjoiUEhNLlJlcG9ydHNASEVBTFRIRUMuQ09NIiwidXBuIjoiUEhNLlJlcG9ydHNASEVBTFRIRUMuQ09NIiwidXRpIjoid2xmZE5aakVoMGFhVlpKMkFlaHVBQSIsInZlciI6IjEuMCJ9.vb0qae_Dkzw_oT4_1YsxHc-UCBGhGoYTMPN5BX0t8f8skLMZUiSupqiQkh-oms2Rg-14qk_jIEvpq9mhZt4HJEJ0w94Tn-RD8GToDlJ0bjRLEkskkUrnLUgudlT-Qa0etD3fyINMgaUVRiN2uNfP2Umipi1gBpVu6-B7HMtRVz2gDIHQBS6_gu8FNYzZMe7Ktc6DAfMPgYW3RUWIeMoiwMFr-u_eTHQheh70xnidkFrw-3L1CGqvh4tZyd3TbiURp3UlXVtM5awg75b4zKLth1_Y0Z-ROOmYh9geo24ytG8i3RxfgnlNgwBfHvrL1jvuysTDNHotbZgQgjNslS7Y4Q";
            $.ajax({
                type: "POST",
                url: 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&amp;lt;=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*/);
                        });
                    });
                }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; When i run application getting error like&amp;nbsp;ypeError: generateEmbedToken(...).then is not a function&lt;BR /&gt;at embedReportAndSetTokenListener.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How could i fix this issue, Please suggest.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2020 15:49:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Refresh-embed-token-using-javascript/m-p/918066#M22177</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-01-31T15:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: Refresh embed token using javascript</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Refresh-embed-token-using-javascript/m-p/1652109#M27732</link>
      <description>&lt;P&gt;Were you able to find any solution.&lt;/P&gt;&lt;P&gt;If yes please post here, I am looking for the same thing.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 14:34:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Refresh-embed-token-using-javascript/m-p/1652109#M27732</guid>
      <dc:creator>nnm</dc:creator>
      <dc:date>2021-02-08T14:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: Refresh embed token using javascript</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Refresh-embed-token-using-javascript/m-p/1995686#M30892</link>
      <description>&lt;P&gt;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.`.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var bearerToken = "Bearer ..."

...

     "Authorization": 'Bearer ${this.bearerToken}'&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Aug 2021 15:01:44 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Refresh-embed-token-using-javascript/m-p/1995686#M30892</guid>
      <dc:creator>AndreiPopa</dc:creator>
      <dc:date>2021-08-03T15:01:44Z</dc:date>
    </item>
  </channel>
</rss>

