Supplies are limited. Contact info@espc.tech right away to save your spot before the conference sells out.
Get your discountScore big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount
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 trying to do is creat a small console app that gets the refresh history for a particular dataset but I am having all sorts of trouble with the access token. Here is the code I have:
private static string GetToken()
{
//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 credential = new ClientCredential(_clientId, _clientSecret);
var authenticationResult = authContext.AcquireToken("https://analysis.windows.net/powerbi/api", credential);
token = authenticationResult.AccessToken;
HttpWebRequest request;
var url = "https://api.powerbi.com/v1.0/myorg/datasets/{DATASETIDHERE}/refreshes/?$top=1";
request = WebRequest.CreateHttp(url);
request.Method = "GET";
var tryItToken = "eyJ0eXAiO etc etc etc";
//Add token to the request header
request.Headers.Add("Authorization", String.Format("Bearer {0}", token));
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
dynamic array = JsonConvert.DeserializeObject(responseString);
}
return token;
}
tryItToken is populated with the token that is generated from here https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/getrefreshhistory .
Now, whenever I pass in the try it token, I get the repsonse back absolutely fine. However, if I replace the tryItToken with the token I get through authenticationcontext.AcquireToken I am receiving the 401 Unauthorized.
Can someone point me in the direction of the steps I have clearly missed out in getting this working correctly from within code?!
Thanks in advance.
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
Good morning @Anonymous
I attempted to do this in a previous attempt but when I tried using the asnyc method, I received the following;
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
Thank you for your hep @Anonymous
Unfortunately it looks like the new definition doesn't like IPlatformParameters
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
Hi,
I am also new. Does the AuthenticationContext fill out the authotityUri correct?
When I use curl I need to call http://login.microsoftonline.com/common/oauth2/token.
Kind regards
Johan