Forum Discussion

allan_oliveira's avatar
allan_oliveira
Frequent Visitor
5 years ago
Solved

REST API Get Access Token

I'm new to the community and using Power BI.
I would like to make a function that updates a token every 1 hour and returns the new token inside a parameter, is it possible??
Could you help me build this function or share some example?

Thanks

  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi allan_oliveira 

    Before you use Rest API to get access token, you need to registered a client app in Azure AD. Every portal has its url to get access token. The resource url for Power BI API is "https://analysis.windows.net/powerbi/api". Then you need to add parameter into your code body, like your Client ID( from your app) or your account and password.

    Here I will show you two ways to get Power BI access token.

    1. Get access token by Postman.

    For reference:  Solved: Power BI REST API using postman - generate embed t... - Microsoft Power BI Community

    2. Try this code to get access token in visual studio by C#.

    For reference: Get an authentication access token

     

    using System;
    using Microsoft.IdentityModel.Clients.ActiveDirectory;
    
    namespace walkthrough_push_data
    {
        class Program
        {
            private static string token = string.Empty;
    
            static void Main(string[] args)
            {
    
                //Get an authentication access token
                token = GetToken();
    
            }
    
            #region Get an authentication access token
            private static async Task<string> GetToken()
            {
                // TODO: Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612
                // and add using Microsoft.IdentityModel.Clients.ActiveDirectory
    
                //The client id that Azure AD created when you registered your client app.
                string clientID = "{Client_ID}";
    
                //RedirectUri you used when you register your app.
                //For a client app, a redirect uri gives Azure AD more details on the application that it will authenticate.
                // You can use this redirect uri for your client app
                string redirectUri = "https://login.live.com/oauth20_desktop.srf";
    
                //Resource Uri for Power BI API
                string resourceUri = "https://analysis.windows.net/powerbi/api";
    
                //OAuth2 authority Uri
                string authorityUri = "https://login.microsoftonline.com/common/";
    
                //Get access token:
                // To call a Power BI REST operation, create an instance of AuthenticationContext and call AcquireToken
                // AuthenticationContext is part of the Active Directory Authentication Library NuGet package
                // To install the Active Directory Authentication Library NuGet package in Visual Studio,
                //  run "Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory" from the nuget Package Manager Console.
    
                // AcquireToken will acquire an Azure access token
                // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
                AuthenticationContext authContext = new AuthenticationContext(authorityUri);
                var token = authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri)).Result.AccessToken;
    
                Console.WriteLine(token);
                Console.ReadLine();
    
                return token;
            }
    
            #endregion
    
        }
    }

     

     

    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. 

11 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi allan_oliveira 

    Before you use Rest API to get access token, you need to registered a client app in Azure AD. Every portal has its url to get access token. The resource url for Power BI API is "https://analysis.windows.net/powerbi/api". Then you need to add parameter into your code body, like your Client ID( from your app) or your account and password.

    Here I will show you two ways to get Power BI access token.

    1. Get access token by Postman.

    For reference:  Solved: Power BI REST API using postman - generate embed t... - Microsoft Power BI Community

    2. Try this code to get access token in visual studio by C#.

    For reference: Get an authentication access token

     

    using System;
    using Microsoft.IdentityModel.Clients.ActiveDirectory;
    
    namespace walkthrough_push_data
    {
        class Program
        {
            private static string token = string.Empty;
    
            static void Main(string[] args)
            {
    
                //Get an authentication access token
                token = GetToken();
    
            }
    
            #region Get an authentication access token
            private static async Task<string> GetToken()
            {
                // TODO: Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612
                // and add using Microsoft.IdentityModel.Clients.ActiveDirectory
    
                //The client id that Azure AD created when you registered your client app.
                string clientID = "{Client_ID}";
    
                //RedirectUri you used when you register your app.
                //For a client app, a redirect uri gives Azure AD more details on the application that it will authenticate.
                // You can use this redirect uri for your client app
                string redirectUri = "https://login.live.com/oauth20_desktop.srf";
    
                //Resource Uri for Power BI API
                string resourceUri = "https://analysis.windows.net/powerbi/api";
    
                //OAuth2 authority Uri
                string authorityUri = "https://login.microsoftonline.com/common/";
    
                //Get access token:
                // To call a Power BI REST operation, create an instance of AuthenticationContext and call AcquireToken
                // AuthenticationContext is part of the Active Directory Authentication Library NuGet package
                // To install the Active Directory Authentication Library NuGet package in Visual Studio,
                //  run "Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory" from the nuget Package Manager Console.
    
                // AcquireToken will acquire an Azure access token
                // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
                AuthenticationContext authContext = new AuthenticationContext(authorityUri);
                var token = authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri)).Result.AccessToken;
    
                Console.WriteLine(token);
                Console.ReadLine();
    
                return token;
            }
    
            #endregion
    
        }
    }

     

     

    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. 

    • Analitika's avatar
      Analitika
      Post Prodigy

      Hello how I could know application token usage in Power BI?

    • Anonymous's avatar
      Anonymous
      Not applicable

      hi, I use the same method and get the error message: 

      "AADSTS90019: No tenant-identifying information found in either the request or implied by any provided credentials.". what should I do?
  • Anonymous's avatar
    Anonymous
    Not applicable

    Thanks for sharing the code. My question is, the above solution is using ADAL libraries rather than MSAL for getting the token. Is it ok to use this above code for long run or will this have any impact as Microsoft is going to retire this ADAL authentication in next couple of months. Please help me if you have the code for MSAL based token acquisition. Thank you so much.