AAD身份验证获取访问令牌

时间:2020-09-25 13:05:10

标签: authentication azure-active-directory azure-authentication

我正在尝试通过以下链接为powerbi用户获取访问令牌-https://docs.microsoft.com/en-us/power-bi/developer/automation/walkthrough-push-data-get-token

但是我在这行代码中遇到了错误

var token = authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri)).Result.AccessToken;

错误:

clinetID为-无法从“字符串”转换为“ microsoft.identitymodel.clients.activedirectory.clientcredential”

新Uri(redirectUri)=无法从'System.uri'转换为'microsoft.identitymodel.clients.activedirectory.User'

获取访问令牌的代码:

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Threading.Tasks;

namespace Service
{
    public class PowerbiService
    {
        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()
       {
        string clientID = "{Client_ID}";

            string redirectUri = "https://login.live.com/oauth20_desktop.srf";

           string resourceUri = "https://analysis.windows.net/powerbi/api";

           string authorityUri = "https://login.microsoftonline.net/common/";

           AuthenticationContext authContext = new AuthenticationContext(authorityUri);
           var token = authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri)).Result.AccessToken;

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

           return token;
       }

       #endregion
    }
}

0 个答案:

没有答案