アクセス トークンを更新する

Power BI コンテンツ (レポート、ダッシュボード、タイル) を埋め込んで操作するには、アクセス トークンが必要です。 アクセス トークンには、organization用に埋め込む場合は Azure AD トークン、顧客用に埋め込む場合は埋め込みトークンのいずれかを指定できます。 アクセス トークンには有効期限があります。つまり、Power BI アイテムを埋め込んだ後、操作する時間は限られています。 ユーザーに継続的なエクスペリエンスを提供するには、有効期限が切れる前にアクセス トークンを更新 (または更新) します。

アクセス トークンを更新するには、次の 2 つの方法があります。

  • API を直接使用するsetAccessToken
  • organizationの埋め込みに Azure AD トークンを使用している場合は自動的

アクセス トークンを直接更新する

setAccessToken を使用して、埋め込みレポートを再読み込みせずにアクセス トークンを更新できます。 トークンの有効期限が迫っているときに使用します。

await report.setAccessToken(newAccessToken);

手動トークン更新の例

アクセス トークンを手動で更新するには、getNewUserAccessToken() を実装します。 この関数は、アプリケーション バックエンドを呼び出して新しい埋め込みトークンを生成するか、Azure AD トークンを更新します。

getNewUserAccessToken() 関数を手動で実装して、有効期限が切れる前にアクセス トークンを更新する方法の例を次に示します。

const MINUTES_BEFORE_EXPIRATION = 10;

// Set the refresh interval time to 30 seconds
const INTERVAL_TIME = 30000;

// Get the token expiration from the access token
var tokenExpiration;

// Set an interval to check the access token expiration, and update if needed
setInterval(() => checkTokenAndUpdate(reportId, groupId), INTERVAL_TIME);

function checkTokenAndUpdate(reportId, groupId) {
    // Get the current time
    const currentTime = Date.now();
    const expiration = Date.parse(tokenExpiration);

    // Time until token expiration in milliseconds
    const timeUntilExpiration = expiration - currentTime;
    const timeToUpdate = MINUTES_BEFORE_EXPIRATION * 60 * 1000;

    // Update the token if it is about to expired
    if (timeUntilExpiration <= timeToUpdate)
    {
        console.log("Updating report access token");
        updateToken(reportId, groupId);
    }
}

async function updateToken(reportId, groupId) {
    // Generate a new embed token or refresh the user Azure AD access token
    let newAccessToken = await getNewUserAccessToken(reportId, groupId);

    // Update the new token expiration time
    tokenExpiration = newAccessToken.expiration;

    // Get a reference to the embedded report HTML element
    let embedContainer = $('#embedContainer')[0];

    // Get a reference to the embedded report.
    let report = powerbi.get(embedContainer);

    // Set the new access token
    await report.setAccessToken(newAccessToken.token);
}

// Add a listener to make sure token is updated after tab was inactive
document.addEventListener("visibilitychange", function() {​​​​
    // Check the access token when the tab is visible
    if (!document.hidden) {​​​​
        checkTokenAndUpdate(reportId, groupId)
    }​​​​
}​​​​);

トークンを自動的に更新する

organization シナリオの埋め込みに Azure AD トークンを使用している場合は、埋め込み構成パラメーターにイベント フックを設定することで、アクセス トークンを自動的に更新できます。 イベント フックは、新しいトークンを生成し、現在のトークンの有効期限が切れる前に、生成されたトークンを埋め込みアイテムに割り当てる関数を呼び出します。 トークン生成関数を提供するだけで、残りは自動的に行われます。

Note

アクセス トークンを自動的に更新することは、powerbi-client JavaScript ライブラリ バージョン 2.20.1 からサポートされています。

アクセス トークンを自動的に更新するには、埋め込み時に 関数を accessTokenProvider パラメーター IEmbedConfiguration として 設定します。 この関数は顧客によって実装され、呼び出されたときに新しいトークンを返します。 トークンの有効期限が近づくと、iframe はフックを accesTokenProvider 呼び出してホスティング アプリから新しいトークンを取得し、新しいトークンを設定します。

トークンの自動更新の例

アクセス トークンの有効期限が切れる前に自動的に更新する方法の例を次に示します。

let getNewAccessToken = async function () {
        // Code you need to add for generating new Azure AD token
        return token;
    };

let config = {
        type: 'report',
        tokenType: models.TokenType.Aad,
        accessToken: “eyJ0 …”,
        embedUrl: “https: …”,
        eventHooks: {
            accessTokenProvider: getNewAccessToken
        }
    };

// Get a reference to the embedded report HTML element
let embedContainer = $('#embedContainer')[0];

// Embed the report and display it within the div container.
report = powerbi.embed(embedContainer, config);

考慮事項と制限事項

  • アクセス トークンの自動更新は、organization (ユーザー所有データ) の埋め込みシナリオでのみサポートされます。
  • イベント フックは accessTokenProvider 例外をスローしないでください。 新しいトークンの生成に失敗した場合は、Null 値を返します。

次のステップ

さまざまな埋め込みソリューションについて