Forum Discussion

IanV's avatar
IanV
Frequent Visitor
8 years ago
Solved

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'...
  • Eric_Zhang's avatar
    8 years ago

    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.