Step 2: Get an authentication access token

Is this page helpful?

This article is the second step in the series Push data into a Power BI dataset.

In step 1, you registered a client app in Azure AD. In this step, you get an authentication access token. Power BI apps are integrated with Azure Active Directory to provide your app with secure sign in and authorization. Your app uses a token to authenticate to Azure AD and gain access to Power BI resources.

Get an authentication access token

Before starting, make sure you've completed the previous step in the Push data into a Power BI dataset series.

This procedure requires Visual Studio 2015 or later.

  1. In Visual Studio, create a new C# Console Application project.

  2. Install the Azure AD Authentication Library for .NET NuGet package. Your .Net app needs this package to get an authentication security token.

    a. Select Tools > NuGet Package Manager > Package Manager Console.

    b. Enter Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612

    c. In Program.cs, add using Microsoft.IdentityModel.Clients.ActiveDirectory;.

  3. Add the sample code listed after these steps to Program.cs.

  4. Replace "{ClientID}", with the Client ID you got in the previous series article when you registered your app.

  5. Run your console app and sign in to your Power BI account.

    A token string should appear in the console window.

Sample code to get authentication security token

Add this code to Program {...}.

  • A token variable to call operations:

    private static string token = string.Empty;
    
    static void Main(string[] args)
    {
    }
    
  • In static void Main(string[] args):

    static void Main(string[] args)
    {
      //Get an authentication access token
      token = GetToken();
    }
    
  • Add a GetToken() method:

       #region Get an authentication access token
       private static 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.net/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);
           string token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri)).AccessToken;

           Console.WriteLine(token);
           Console.ReadLine();

           return token;
       }

       #endregion

After you get an authentication token, you can call any Power BI operation.

The next article in this series shows you how to Create a dataset in Power BI.

Complete code listing

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 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);
            string token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri)).AccessToken;

            Console.WriteLine(token);
            Console.ReadLine();

            return token;
        }

        #endregion

    }
}

Next steps

Next article in this series > Create a dataset in Power BI

Overview of Power BI REST API
Power BI REST APIs

More questions? Try the Power BI Community