问题描述
我有将我登录到Azure AD的代码,但是我无法弄清楚如何获取访问令牌以调用REST API或PowerBI
解决方法
有用于获取Power BI REST API访问令牌的示例代码。
//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/";
// 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);
有关更多详细信息,请参见here。