<?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 Power BI embed + token that never expires + javascript SDK in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-embed-token-that-never-expires-javascript-SDK/m-p/751731#M20139</link>
    <description>&lt;P&gt;Re:&amp;nbsp;&lt;A href="https://community.powerbi.com/t5/Developer/Integrate-PowerBI-with-Netsuite/td-p/92420" target="_blank"&gt;https://community.powerbi.com/t5/Developer/Integrate-PowerBI-with-Netsuite/td-p/92420&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) How do I generate a token that never expires?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to use it using this sample html code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&amp;lt;html&amp;gt;

 &amp;lt;script src="https://microsoft.github.io/PowerBI-JavaScript/demo/node_modules/jquery/dist/jquery.js"&amp;gt;&amp;lt;/script&amp;gt; 
 &amp;lt;script src="https://microsoft.github.io/PowerBI-JavaScript/demo/node_modules/powerbi-client/dist/powerbi.js"&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;script type="text/javascript"&amp;gt;
window.onload = function () {

var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id,Convert.ToDateTime("2099-12-31");

var embedConfiguration = {
 type: 'report',
 accessToken: '',
 id: 'report ID here',
 embedUrl: 'https://app.powerbi.com/reportEmbed?reportId='
}; 

var $reportContainer = $('#reportContainer');
 
var report = powerbi.embed($reportContainer.get(0), embedConfiguration);


}
&amp;lt;/script&amp;gt;

&amp;lt;div id="reportContainer"&amp;gt;&amp;lt;/div&amp;gt;

&amp;lt;/html&amp;gt;&lt;/PRE&gt;&lt;P&gt;or&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) Can anyone help put together HTML sample and integrate the above code with:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/Microsoft/PowerBI-JavaScript/wiki/Refresh-token-using-JavaScript-SDK-example" target="_blank"&gt;https://github.com/Microsoft/PowerBI-JavaScript/wiki/Refresh-token-using-JavaScript-SDK-example&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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&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;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 29 Jul 2019 16:45:52 GMT</pubDate>
    <dc:creator>diskovered</dc:creator>
    <dc:date>2019-07-29T16:45:52Z</dc:date>
    <item>
      <title>Power BI embed + token that never expires + javascript SDK</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-embed-token-that-never-expires-javascript-SDK/m-p/751731#M20139</link>
      <description>&lt;P&gt;Re:&amp;nbsp;&lt;A href="https://community.powerbi.com/t5/Developer/Integrate-PowerBI-with-Netsuite/td-p/92420" target="_blank"&gt;https://community.powerbi.com/t5/Developer/Integrate-PowerBI-with-Netsuite/td-p/92420&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) How do I generate a token that never expires?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to use it using this sample html code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&amp;lt;html&amp;gt;

 &amp;lt;script src="https://microsoft.github.io/PowerBI-JavaScript/demo/node_modules/jquery/dist/jquery.js"&amp;gt;&amp;lt;/script&amp;gt; 
 &amp;lt;script src="https://microsoft.github.io/PowerBI-JavaScript/demo/node_modules/powerbi-client/dist/powerbi.js"&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;script type="text/javascript"&amp;gt;
window.onload = function () {

var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id,Convert.ToDateTime("2099-12-31");

var embedConfiguration = {
 type: 'report',
 accessToken: '',
 id: 'report ID here',
 embedUrl: 'https://app.powerbi.com/reportEmbed?reportId='
}; 

var $reportContainer = $('#reportContainer');
 
var report = powerbi.embed($reportContainer.get(0), embedConfiguration);


}
&amp;lt;/script&amp;gt;

&amp;lt;div id="reportContainer"&amp;gt;&amp;lt;/div&amp;gt;

&amp;lt;/html&amp;gt;&lt;/PRE&gt;&lt;P&gt;or&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) Can anyone help put together HTML sample and integrate the above code with:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/Microsoft/PowerBI-JavaScript/wiki/Refresh-token-using-JavaScript-SDK-example" target="_blank"&gt;https://github.com/Microsoft/PowerBI-JavaScript/wiki/Refresh-token-using-JavaScript-SDK-example&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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&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;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2019 16:45:52 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-embed-token-that-never-expires-javascript-SDK/m-p/751731#M20139</guid>
      <dc:creator>diskovered</dc:creator>
      <dc:date>2019-07-29T16:45:52Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI embed + token that never expires + javascript SDK</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-embed-token-that-never-expires-javascript-SDK/m-p/752973#M20166</link>
      <description>&lt;P&gt;Anyone? &lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/6971"&gt;@Eric_Zhang&lt;/a&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 15:36:00 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-embed-token-that-never-expires-javascript-SDK/m-p/752973#M20166</guid>
      <dc:creator>diskovered</dc:creator>
      <dc:date>2019-07-30T15:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI embed + token that never expires + javascript SDK</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-embed-token-that-never-expires-javascript-SDK/m-p/919782#M22194</link>
      <description>&lt;P&gt;facing the same issu, dont understand how shuld i use the&amp;nbsp;javascript-SDK code&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 14:54:29 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-embed-token-that-never-expires-javascript-SDK/m-p/919782#M22194</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-02-03T14:54:29Z</dc:date>
    </item>
  </channel>
</rss>

