重新整理存取權杖

(報表、儀表板和磚) 內嵌 Power BI 內容並與其互動,需要存取權杖。 當您為組織內嵌時,存取權杖可以是 Azure AD 權杖,或在為客戶內 嵌時內嵌權杖。 存取權杖有到期時間,這表示在內嵌 Power BI 專案之後,您有有限的時間來與其互動。 若要讓使用者持續體驗,請在存取權杖到期之前,重新整理 (或更新) 權杖。

有兩種方式可以重新整理您的存取權杖:

  • 直接 使用 setAccessToken API
  • 如果您使用 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)
    }​​​​
}​​​​);

自動重新整理權杖

如果您使用 Azure AD 權杖進行組織案例的 內嵌 ,您可以在內嵌組態參數中設定事件勾點,以自動重新整理存取權杖。 事件攔截會呼叫產生新權杖的函式,並將產生的權杖指派給內嵌專案,再讓目前權杖到期。 您只需要提供權杖產生函式,其餘部分就會自動發生。

注意

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);

考量與限制

  • 只有 組織內嵌 支援自動重新整理存取權杖, (使用者擁有資料) 案例。
  • 事件 accessTokenProvider 攔截絕對不應該擲回例外狀況。 如果無法產生新的權杖,請傳回 Null 值。

後續步驟

瞭解不同的內嵌解決方案