cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
IanV Frequent Visitor
Frequent Visitor

Authenticating with C# API V2 against Azure Power BI Workspaces

The new Power BI API under the Microsoft.PowerBI.Api.V2 namespace has some additional functionality that I'd like to leverage, but I can't seem to get it to authenticate for the life of me.

 

Here's an example of how I created a client with V1:

 

 

var apiEndpoint = new Uri("https://api.powerbi.com");
var accessKey = "thisvalueisprovidedbyazure";

var credentials = new TokenCredentials(accessKey, "AppKey");
var clientV1 = new Microsoft.PowerBI.Api.V1.PowerBIClient(apiEndpoint, credentials);

As implied by the fake value, that access key is provided by Azure, as you can see in "Get started with Microsoft Power BI Embedded" article here: https://docs.microsoft.com/en-us/azure/power-bi-embedded/power-bi-embedded-get-started

 

I could then use it with a workspaceId and collectionName (also created in Azure):

var collectionName  = "my-power-bi-collection";
var workspaceId = "00000000-0000-0000-0000-000000000000";

var datasets = await client.Datasets.GetDatasetsAsync(collectionName, workspaceId);

The new API seems to be completely different, however. I tried following the example in this article and the one in the git repository readme file, but it wasn't very clear where some of these values are coming from. They seem geared toward generating an access token via username/password or AD.

 

In the example from github, it's not very clear what some of these values are. For example, what is the ClientId? Why is the AuthorityUrl pointing to "https://login.windows.net/common/oauth2/authorize/"? Is that required for Azure? There is also no mention of workspaces (they seem to have been replaced by groups?)

 

Rather than generating a new token, I tried using the token provided by Azure, and it gives me a 403 FORBIDDEN every time.

 

Does anyone know what is required to get the new API running against Azure Power BI workspaces?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Moderator Eric_Zhang
Moderator

Re: Authenticating with C# API V2 against Azure Power BI Workspaces

@IanV

The Power BI Embedded workspace collection is being deprecated. The new replacement is Embedding with non-Power BI users (app owns data). The correlated github demo is App Owns Data. See authentication code snippet below.

 

// Create a user password cradentials.
            var credential = new UserPasswordCredential(Username, Password);

            // Authenticate using created credentials
            var authenticationContext = new AuthenticationContext(AuthorityUrl);
            var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);

            if (authenticationResult == null)
            {
                return View(new EmbedConfig()
                {
                    ErrorMessage = "Authentication Failed."
                });
            }

            var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");

The username and password are of one Power BI Pro account. The client_id is from a registered Azure AD application. See this tutorial.

 

 

View solution in original post

2 REPLIES 2
Moderator Eric_Zhang
Moderator

Re: Authenticating with C# API V2 against Azure Power BI Workspaces

@IanV

The Power BI Embedded workspace collection is being deprecated. The new replacement is Embedding with non-Power BI users (app owns data). The correlated github demo is App Owns Data. See authentication code snippet below.

 

// Create a user password cradentials.
            var credential = new UserPasswordCredential(Username, Password);

            // Authenticate using created credentials
            var authenticationContext = new AuthenticationContext(AuthorityUrl);
            var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);

            if (authenticationResult == null)
            {
                return View(new EmbedConfig()
                {
                    ErrorMessage = "Authentication Failed."
                });
            }

            var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");

The username and password are of one Power BI Pro account. The client_id is from a registered Azure AD application. See this tutorial.

 

 

View solution in original post

IanV Frequent Visitor
Frequent Visitor

Re: Authenticating with C# API V2 against Azure Power BI Workspaces

Thanks, I didn't realize that v2 of the API was completely incompatible with the original workspaces. I'll go ahead and give the new way a try.






The Power BI Embedded workspace collection is being deprecated.


That's what I was afraid of. Do you know how long the existing Azure workspaces will be available and how long they'll be accessible vai API v1?

Helpful resources

Announcements
New Kudos Given Badges Coming

New Kudos Given Badges Coming

We're rolling out new Kudos Given badges. Find out how many Kudos you've given.

Power Platform World Tour

Power Platform World Tour

Find out where you can attend!

Top Solution Authors
Top Kudoed Authors (Last 30 Days)
Users online (4,413)