Forum Discussion
problem getting authentication token from AAD
- 8 years ago
Hi v-ljerr-msft,
It seems that the issue was related to deadlocking. Found couple ways to fix it:
1) Add ConfigureAwait(false) at the end of AcquireTokenAsync call;
2) Use RegisterAsyncTask method in page_load event.
Thanks
Hi m4pv,
Could you also try the code (provided by BennyM) below to see if it works in your scenario? :smileyhappy:
- Source & Instructions: https://github.com/BennyM/PowerBI-CSharp
- Package: https://www.nuget.org/packages/PowerBI.Api/
private async Task<TokenCredentials> GetAccessToken()
{
using (HttpClient client = new HttpClient())
{
string tenantId = "";
var tokenEndpoint = "";
var accept = "application/json";
var userName = "";
var password = "";
var clientId = "";
client.DefaultRequestHeaders.Add("Accept", accept);
string postBody = null;
postBody = $@"resource=https%3A%2F%2Fanalysis.windows.net/powerbi/api
&client_id={clientId}
&grant_type=password
&username={userName}
&password={password}
&scope=openid";
var tokenResult = await client.PostAsync(tokenEndpoint, new StringContent(postBody, Encoding.UTF8, "application/x-www-form-urlencoded"));
tokenResult.EnsureSuccessStatusCode();
var tokenData = await tokenResult.Content.ReadAsStringAsync();
JObject parsedTokenData = JObject.Parse(tokenData);
var token = parsedTokenData["access_token"].Value<string>();
return new TokenCredentials(token, "Bearer");
}
}
public async Task<IActionResult> Index()
{
var tokenCredentials = await GetAccessToken();
// Create a Power BI Client object (it will be used to call Power BI APIs)
using (var powerBiclient = new PowerBIClient(new Uri("https://api.powerbi.com/"), tokenCredentials))
{
// Get a list of all groupts
var reports = powerBiclient.Groups.GetGroups();
// Do anything you want with the list of groups.
}
return View();
}
Regards
Hi v-ljerr-msft,
It seems that the issue was related to deadlocking. Found couple ways to fix it:
1) Add ConfigureAwait(false) at the end of AcquireTokenAsync call;
2) Use RegisterAsyncTask method in page_load event.
Thanks
- v-ljerr-msft8 years agoMicrosoft Employee
Hi m4pv,
Great to hear the problem got resolved! Could you accept your reply above as solution to close this thread, which could also help others who have similar issue easily find the solution? :smileyhappy:
Regards