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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
rdc_green
Frequent Visitor

Get an authentication access token

I have registered an app with Azure AD and now have an Application ID (Register an Azure AD application to use with Power BI) i've now tried to complete step 2 of the process, which is to Get an authentiaction access token I have downloaded Visual Studio 2019 onto my laptop and followed the steps in the documentation, but I get the following errors:

 

CS0246  The type or namespace name 'Task<>' could not be found (are you missing a using directive or an assembly reference?) 

CS1503 Argument 3: cannot convert from 'System.Uri' to 'Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential'

 

I can sort out the first error by changing the line below:

 

private static async Task<string> GetToken()

 

to:

 

private static string GetToken()

 

 

But i can't figure out how to correct the second issue.

 

Has anybody else come across this problem. Thank you

 

5 REPLIES 5
rdc_green
Frequent Visitor

Hi @Anonymous i've done the following:

 

Home/App Registrations/{App Name}

API Permissions

Grant Admin Consent for {Directory}

 

Result - 'Successfully granted admin consent for the requested permissions' 

 

I've tried the following in Postman again with the same result:

 

client_id - {Application (client) ID}

grant_type - password

resource - https://analysis.windows.net/powerbi/api

username - {my username}

password - {my password}

 

In 'API Permissions' I have 'Azure Active Directory Graph' and 'Power BI Service'. Am I missing any other permissions?

 

Thanks

 

Anonymous
Not applicable

Hi @rdc_green 

Way 1:

In the sample, steps 2 is using

Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612

Please make sure you have installed the package correctly.

In the newer ADAL v3, try this sample code.

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication32
{
    class Program
    { 
        static void Main(string[] args)
        {
            string token =  GetToken();
            Console.WriteLine(token);             
            Console.ReadLine();
             
        }

        #region Get an authentication access token
        private  static string GetToken()
        {
            // TODO: Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612
            // and add using Microsoft.IdentityModel.Clients.ActiveDirectory

            //The client id that Azure AD created when you registered your client app.
            string clientID = "3f756axxxxxxx3662e";

            //RedirectUri you used when you register your app.
            //For a client app, a redirect uri gives Azure AD more details on the application that it will authenticate.
            // You can use this redirect uri for your client app
            string redirectUri = "https://login.live.com/oauth20_desktop.srf";

            //Resource Uri for Power BI API
            string resourceUri = "https://analysis.windows.net/powerbi/api";

            //OAuth2 authority Uri
            string authorityUri = "https://login.windows.net/common/oauth2/authorize";

            //Get access token:
            // To call a Power BI REST operation, create an instance of AuthenticationContext and call AcquireToken
            // AuthenticationContext is part of the Active Directory Authentication Library NuGet package
            // To install the Active Directory Authentication Library NuGet package in Visual Studio,
            //  run "Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory" from the nuget Package Manager Console.

            // AcquireToken will acquire an Azure access token
            // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
            AuthenticationContext authContext = new AuthenticationContext(authorityUri);

            //string token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri)).AccessToken;

            return   authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri), new PlatformParameters(0)).Result.AccessToken;
             
            
             
        }

        #endregion
    }
}

 

Way 2. Or you can have a test on getting access token or generate embed token by postman.

Enter the same link and body in postman.

RicoZhou_0-1625470251261.png

 

For reference: Solved: Power BI REST API using postman - generate embed t... - Microsoft Power BI Community

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

Thank you @Anonymous for your suggestions I have attempted both ways and have the following issues:

 

Way 1

I have the following 4 errors

  1. Error CS0017 Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. 
  2. Error CS0103 The name 'token' does not exist in the current context ConsoleApp1 
  3. Error CS0103 The name 'GetToken' does not exist in the current context ConsoleApp1 
  4. Error CS7036 There is no argument given that corresponds to the required formal parameter 'customWebUi' of 'PlatformParameters.PlatformParameters(PromptBehavior, ICustomWebUi)'

 

Way 2

I have the following error due to MFA

 

"AADSTS50076: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access '00000009-0000-0000-c000-000000000000'.\r\nTrace ID: 1134cdb0-67cf-4721-8611-15879e9fef00\r\nCorrelation ID: 56e71b1d-a011-4451-b385-87af8e3874c7\r\nTimestamp: 2021-07-05 08:11:03Z",
 
Do you have any suggestions to fix either of these ways?
 
Thank you
Richard

 

Anonymous
Not applicable

Hi @rdc_green 

Please try below steps to fix Way 2:

1.Login as a tenant admin to https://portal.azure.com
2.Open the registration for your app in the
3.Go to Settings then Required Permissions
4.Press the Grant Permissions button

Note: If you are not a tenant admin, you cannot give admin consent

You may try the solutions provided in similar GitHub issue.

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

I managed to get my access token by disabling MFA in AAD

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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