Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate to Azure Active Directory without user interaction?

I wish to obtain an access token without user interaction in order to automate REST API calls to Azure services (Compute, Network, etc.). In the documentation, several authentication scenarios are listed. The one that fits my use-case the most is "Daemon or Server Application to Web API".

I followed the instructions to request an access token : first I registered an App in my Azure Active Directory. Then I created a key (client_secret parameter) associated to the application ID (client_id parameter). I also obtained the App ID URI (resource parameter). I used all these parameters to create a POST request to the /token endpoint of my Azure AD. However I get the following error message:

{
    "code":"InvalidAuthenticationTokenAudience",
    "message":"The access token has been obtained from wrong audience or resource 'https://solutionsmosaixsoft.onmicrosoft.com/<APP_ID_URI>'. It should exactly match (including forward slash) with one of the allowed audiences 'https://management.core.windows.net/','https://management.azure.com/'."
}

What am I doing wrong? Am I setting the resource parameter to the wrong value?

like image 280
Gleb Billig Avatar asked Mar 15 '17 23:03

Gleb Billig


1 Answers

The resource parameter tells your application where to get token(identifier of the resource for which the access token is being requested). If you want to get a token to call Azure Service Management API , you could set the resource as https://management.core.windows.net/ .

Edit:

If the APIs you want to call are Microsoft provided APIs , the resource is known ,for example :

  • Azure Service Management API:https://management.core.windows.net/
  • Microsoft Graph API :https://graph.microsoft.com/

If you want to call the API apps which your created , you can either use the Client ID or the App ID URI of the resource WebAPI (Find them in the configure tab of the Azure AD application in the Azure Management portal).

You could refer to below link for code samples :

https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-code-samples

like image 162
Nan Yu Avatar answered Oct 16 '22 20:10

Nan Yu