Forum Discussion

uahmed114pbi's avatar
uahmed114pbi
Frequent Visitor
2 years ago
Solved

To get Power BI API Access Token dynamically, make sure to do this

Im sure many people have struggled here to get this answer, so have I. I was finally able to resolve it with the help of microsoft support. I saw many posts here but none had the solution or had part...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi uahmed114pbi 

     

    Thank you for your sharing. I think we can try code as below to get access token in Power Query Editor. Then use this token as parameter when you get data by REST API. Access token will refresh new each time you refresh your report.

     

    1. Get Access token in Power BI Desktop:

     

     

    () =>
    
     
    
    let
    
        body = "client_id=" & #"App ID"
    
        & "&scope=https://analysis.windows.net/powerbi/api/.default&client_secret=" & #"App Secret"
    
        & "&grant_type=client_credentials",
    
        Data= Json.Document(Web.Contents("https://login.microsoftonline.com/"& TenantID & "/oauth2/v2.0/token/",
    
        [Headers=[#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(body)])),
    
        access_token = Data[access_token]
    
    in
    
        access_token

     

     

    1. Video: How to get access token and call Rest API by dynamic access token in Power BI Desktop?


    Best Regards,
    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.