액세스 토큰 새로 고침

Power BI 콘텐츠(보고서, 대시보드 및 타일)를 포함하고 상호 작용하려면 액세스 토큰이 필요합니다. 액세스 토큰은 organization 포함할 때 Azure AD 토큰이거나 고객을 위해 포함할 때 포함 토큰일 수 있습니다. 액세스 토큰에는 만료 시간이 있습니다. 즉, Power BI 항목을 포함하면 상호 작용하는 데 시간이 제한됩니다. 사용자에게 지속적인 환경을 제공하려면 만료되기 전에 액세스 토큰을 새로 고치거나 갱신합니다.

액세스 토큰을 새로 고치는 방법에는 두 가지가 있습니다.

  • 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 토큰을 사용하는 경우 포함 구성 매개 변수에 이벤트 후크를 설정하여 액세스 토큰을 자동으로 새로 고칠 수 있습니다. 이벤트 후크는 새 토큰을 생성하는 함수를 호출하고 현재 토큰이 만료되기 전에 생성된 토큰을 포함된 항목에 할당합니다. 토큰 생성 함수만 제공하면 나머지는 자동으로 수행됩니다.

참고

액세스 토큰을 자동으로 새로 고치는 것은 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 이벤트 후크는 예외를 throw해서는 안 됩니다. 새 토큰을 생성하지 못하면 Null 값을 반환합니다.

다음 단계

다양한 포함 솔루션 이해