Aleksa
Aleksa

Reputation: 3124

Office 365 APIs Microsoft Graph authentication failed

I registered sample app from Microsoft graph sample app

And standard login is working but when I try to make it simplier by using this code:

        var authContext = new AuthenticationContext(Settings.AzureADAuthority);

        var token = authContext.AcquireToken(Settings.O365UnifiedAPIResource, new ClientCredential(Settings.ClientId, Settings.ClientSecret)).AccessToken;

I get the following error: Application with identifier '[ClientId here]' was not found in the directory microsoft.com

Setting.O365UnifiedAPIResource = @"https://graph.microsoft.com/";
Settings.AzureADAuthority = @"https://login.microsoftonline.com/common";

Does anyone know what can be the problem?

Upvotes: 0

Views: 772

Answers (1)

Jeffrey Chen
Jeffrey Chen

Reputation: 4690

Settings.AzureADAuthority = @"https://login.microsoftonline.com/{tenant_id or tenant_name}";

When acquiring the token by using the client credential (client id + client secret). You should specify the tenant explicitly.

For example:

https://login.microsoftonline.com/{tenant_id}

or

https://login.microsoftonline.com/{your_domain.onmicrosoft.com}

BTW, as this registration will be for the sample app, it will only have the Mail.Send permission which is delegated permission. To acquire the app token, you also need to grant the app level permission in Azure AD since your are acquiring the app token rather than the user token.

enter image description here

Upvotes: 2

Related Questions