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,
I'd like to suggest you take a look at the following tutorial about getting an access token if it works on your side:
Get an authentication access token
Regards,
Xiaoxin Sheng
- MrRichDean5 years agoRegular Visitor
Good morning Anonymous
I attempted to do this in a previous attempt but when I tried using the asnyc method, I received the following;
- Anonymous5 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
- MrRichDean5 years agoRegular Visitor
Thank you for your hep Anonymous
Unfortunately it looks like the new definition doesn't like IPlatformParameters