Forum Discussion
MrRichDean
5 years agoRegular Visitor
REST Api - Access Token 'The remote server returned an error: (401) Unauthorized.'
Good morning / afternoon / evening everyone First of all, apologies for my ignorance and for any stupid questions I ask. I'm new to Power BI so very much finding my feet. Ok, so what I am try...
Anonymous
5 years agoNot applicable
Hi MrRichDean,
It seems like the new version NuGet pack changes the defined so that the old version code not working. Please try to use the following code replaces the raw part if it works on your side:
private static string GetToken()
{
// 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.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri), (IPlatformParameters)new UserCredential()).Result.AccessToken;
Console.WriteLine(token);
Console.ReadLine();
return token;
}
Regards,
Xiaoxin Sheng
MrRichDean
5 years agoRegular Visitor
Thank you for your hep Anonymous
Unfortunately it looks like the new definition doesn't like IPlatformParameters
- Anonymous5 years agoNot applicable
Hi MrRichDean,
What version of the NuGet pack are you worked? I direct copy the NuGet pack name (from document) to vs studio package manager then downloads and test with the last version.
Regards,
Xiaoxin Sheng