Search code examples
powerbiazure-active-directorypowerbi-embedded

Dynamically generate Powerbi Access Token (AAD Token)


i'm using powerbi rest APIs in my react app.

for authentication I'm using copying static Azure Ad token (not an embed token) from their developers playground.

The token is only valid for an hour.

is there any way to dynamically generate it?


Solution

  • I tried to reproduce the same in my environment and got the results like below:

    Based on your requirement you can add the API permissions:

    enter image description here

    I created an Azure AD Application and added API permission like below:

    enter image description here

    Offline_access API permission is required to generate the refresh token.

    I generated the access token using below parameters:

    GET https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
    
    client_id:ClientID
    client_secret:*****
    scope:RoleManagement.ReadWrite.Directory
    grant_type:authorization_code
    redirect_uri:RedirectUri
    code:code
    

    enter image description here

    To refresh the access token, I used below parameters:

    GET https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
    
    client_id:ClientID
    grant_type:refresh_token
    refresh_token:refreshtoken
    client_secret:*****
    

    enter image description here

    Reference:

    Create a React single-page app that uses auth code flow - Microsoft Entra