Forum Discussion

janfcarl's avatar
janfcarl
Helper I
5 years ago

Problem with PowerBIClient (GetReportsInGroupAsync)

EDIT:

I was able to fix the problem by adding

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

to the method GetTokenCredentials.

 

My next problem is that the call

var requestParameters = new GenerateTokenRequest();

var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(workspaceID, id, requestParameters);

throws an other error: 

Operation returned an invalid status code 'Forbidden'

We are hoping to clarify on this with Microsoft directly.

 

We are having problems with connecting to Power BI Service from within an .Net web application. The c# code being used is

 

var tokenCredentials = await GetTokenCredentials(authorityUrl, clientID, resourceUrl);

//resourceUrl = https://analysis.windows.net/powerbi/api

...

var apiUri = new Uri(apiUrl);  //apiUrl = https://api.powerbi.com/

using (var client = new PowerBIClient(apiUri, tokenCredentials))
{
var reportList = await client.Reports.GetReportsInGroupAsync(workspaceID);
...
}

 

The last line of code throws an error:

An existing connection was forcibly closed by the remote host

Can anyone tell or guess what the problem is? The user we use to receive token credentials is a Power BI pro user.

 

private static async Task<TokenCredentials> GetTokenCredentials(string authorityUrl, string clientID, string resourceUrl)
{
var credential = new UserPasswordCredential(azureADUserName, azureADPassword);
var context = new AuthenticationContext(authorityUrl, TokenCache.DefaultShared);
var result = await context.AcquireTokenAsync(resourceUrl, clientID, credential);

if (result == null)
return null;

return new TokenCredentials(result.AccessToken, "Bearer");
}

3 Replies