Krok 2: Získanie overovacieho prístupového tokenuStep 2: Get an authentication access token

Tento článok je druhý krok v sérii Presunutie údajov do množiny údajov v službe Power BI.This article is the second step in the series Push data into a Power BI dataset.

V kroku 1 ste zaregistrovali klientsku aplikáciu v službe Azure AD.In step 1, you registered a client app in Azure AD. V tomto kroku získate overovací prístupový token.In this step, you get an authentication access token. Aplikácie služby Power BI sú integrované so službou Azure Active Directory, čo vašej aplikácii poskytuje bezpečné prihlasovanie a overenie.Power BI apps are integrated with Azure Active Directory to provide your app with secure sign in and authorization. Aplikácia používa token na overovanie v službe Azure AD a na získanie prístupu k zdrojom služby Power BI.Your app uses a token to authenticate to Azure AD and gain access to Power BI resources.

Získanie overovacieho prístupového tokenuGet an authentication access token

Skôr ako začnete, uistite sa, že ste absolvovali predchádzajúci krok v sérii Presunutie údajov do množiny údajov v službe Power BI.Before starting, make sure you've completed the previous step in the Push data into a Power BI dataset series.

Tento postup vyžaduje Visual Studio 2015 alebo novšiu verziu.This procedure requires Visual Studio 2015 or later.

  1. Vo Visual Studiu vytvorte nový projekt v jazyku C# pre konzolovú aplikáciu.In Visual Studio, create a new C# Console Application project.

  2. Nainštalujte knižnicu overenia Azure AD pre balík .NET NuGet.Install the Azure AD Authentication Library for .NET NuGet package. Ak chcete získať overovací token zabezpečenia, vaša aplikácia .NET bude potrebovať tento balík.Your .Net app needs this package to get an authentication security token.

    a)a. Vyberte položku Nástroje > Správca balíka NuGet > Konzola správca balíka.Select Tools > NuGet Package Manager > Package Manager Console.

    b.b. Zadajte inštalačný balík Microsoft.IdentityModel.Clients.ActiveDirectory - verzia 2.21.301221612Enter Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612

    c.c. Do súboru Program.cs, pridajte using Microsoft.IdentityModel.Clients.ActiveDirectory;.In Program.cs, add using Microsoft.IdentityModel.Clients.ActiveDirectory;.

  3. Do súboru Program.cs pridajte vzorový kód uvedený za týmito krokmi.Add the sample code listed after these steps to Program.cs.

  4. Nahraďte „{ClientID}“ položkou Client ID, ktorú ste našli v článku predchádzajúcej série, keď ste si registrovali svoju aplikáciu.Replace "{ClientID}", with the Client ID you got in the previous series article when you registered your app.

  5. Spustite svoju konzolovú aplikáciu a prihláste sa do svojho konta služby Power BI.Run your console app and sign in to your Power BI account.

    V okne konzoly by sa mal zobraziť reťazec tokenu.A token string should appear in the console window.

Vzorový kód na získanie overovacieho tokenu zabezpečeniaSample code to get authentication security token

Tento kód pridajte do súboru Program {...}.Add this code to Program {...}.

  • Premenná tokenu na zavolanie operácií:A token variable to call operations:

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

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

       #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.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);
           var token = authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri)).Result.AccessToken;

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

           return token;
       }

       #endregion

Po získaní overovacieho tokenu môžete zavolať akúkoľvek operáciu služby Power BI.After you get an authentication token, you can call any Power BI operation.

Nasledujúci článok v tejto sérii ukazuje, ako Vytvoriť množinu údajov v službe Power BI.The next article in this series shows you how to Create a dataset in Power BI.

Úplný zoznam kódovComplete 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 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

    }
}

Ďalšie krokyNext steps

Ďalšie otázky.More questions? Skúste sa spýtať v komunite Power BI.Try the Power BI Community