Forum Discussion
Get an authentication access token
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.
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.
- rdc_green5 years agoFrequent Visitor
Thank you Anonymous for your suggestions I have attempted both ways and have the following issues:
Way 1
I have the following 4 errors
- Error CS0017 Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
- Error CS0103 The name 'token' does not exist in the current context ConsoleApp1
- Error CS0103 The name 'GetToken' does not exist in the current context ConsoleApp1
- 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 youRichard- Anonymous5 years agoNot 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.
- rdc_green5 years agoFrequent Visitor
I managed to get my access token by disabling MFA in AAD