Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
IanV
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
Eric_Zhang
Employee
Employee

@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
Eric_Zhang
Employee
Employee

@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.

 

 

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.