步驟 2:取得驗證存取權杖Step 2: Get an authentication access token

本文屬於將資料推送至資料集逐步解說的一部分。This article is part of a step-by-step walkthrough to push data into a dataset.

在<將資料推送至資料集>的步驟 1 使用 Azure AD 註冊應用程式中,您在 Azure AD 中註冊了用戶端應用程式。In step 1 of Push data into a dataset, Register the app with Azure AD, you registered a client app in Azure AD. 在此步驟中,您會收到驗證存取權杖。In this step, you get an authentication access token. Power BI 應用程式會與 Azure AD 整合,以提供應用程式的安全登入和授權。Power BI apps are integrated with Azure AD to provide secure sign in and authorization for your app. 您可以使用權杖來向 Azure AD 驗證,並取得 Power BI 資源的存取權。You use a token to authenticate to Azure AD and gain access to Power BI resources.

以下是取得驗證存取權杖的方式。Here's how to get an authentication access token.

取得驗證存取權杖Get an authentication access token

注意:開始使用之前,請確定您已經遵循將資料推送至資料集逐步解說中的先前步驟。NOTE: Before you get started, make sure you have followed the previous steps in the push data into a dataset walkthrough.

  1. 在 Visual Studio 2015 中,建立 主控台應用程式 專案。In Visual Studio 2015, create a Console Application project.
  2. 安裝 .NET NuGet 套件的 Azure AD 驗證程式庫Install the Azure AD Authentication Library for .NET NuGet package. 若要取得 .NET 應用程式中的驗證安全性權杖,您可以使用此套件。To get an authentication security token in a .NET app, you use this package. 安裝套件的方法如下:Here's how to install the package:

    a.a. 在 Visual Studio 2015 中,選擇 工具 > NuGet 套件管理員 > 套件管理器主控台In Visual Studio 2015, choose Tools > NuGet Package Manager > Package Manager Console.

    b.b. 套件管理器主控台中,輸入 Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612。In Package Manager Console, enter Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612.

  3. 將下列程式碼加入類別 Program {...}。Add the code below into class Program {...}.
  4. 將 "{ClientID}" 取代為註冊應用程式時所得的 用戶端識別碼Replace "{ClientID}", with the Client ID you got when you registered the app. 請參閱使用 Azure AD 註冊應用程式See Register the app with Azure AD.
  5. 安裝 Microsoft.IdentityModel.Clients.ActiveDirectory 套件之後,將 using Microsoft.IdentityModel.Clients.ActiveDirectory; 加入 Program.cs。After installing the Microsoft.IdentityModel.Clients.ActiveDirectory package, add using Microsoft.IdentityModel.Clients.ActiveDirectory; to Program.cs.
  6. 執行主控台應用程式,然後登入您的 Power BI 帳戶。Run the Console App, and login to your Power BI account. 您應該會在 [主控台視窗] 中看到權杖字串。You should see a token string in the Console Window.

取得驗證安全性權杖的範例程式碼Sample code to get authentication security token

將此程式碼加入 Program {...}。Add this code to Program {...}.

  • 呼叫作業的權杖變數:A token variable to call operations:

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

    static void Main(string[] args)
    {
      //Get an authentication access token
      token = GetToken();
    }
    
  • 加入 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.windows.net/common/oauth2/authorize";

           //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

取得驗證權杖之後,您可以呼叫任何 Power BI 作業。After you get an authentication token, you can call any Power BI operation. 下一個步驟將為您示範如何呼叫 PostDataset 作業,以建立將資料推送至儀表板的資料集。The next step shows you how to call the PostDataset operation to create a dataset to push data into a dashboard.

下一個步驟將為您示範如何在 Power BI 中建立資料集The next step shows you how to create a dataset in Power BI.

以下是完整程式碼清單Below is the complete code listing.

完整程式碼清單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.windows.net/common/oauth2/authorize";

            //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 Step >

後續步驟Next steps

在 Power BI 中建立資料集Create a dataset in Power BI
使用 Azure AD 註冊應用程式Register an app with Azure AD
Azure AD Authentication Library for .NET NuGet 套件Azure AD Authentication Library for .NET NuGet package
將資料推送至 Power BI 資料集Push data into a Power BI dataset
Power BI REST API 概觀Overview of Power BI REST API
Power BI REST API 參考Power BI REST API reference
有其他問題嗎?More questions? 試試 Power BI 社群Try the Power BI Community