Angelo Guarino
Angelo Guarino

Reputation: 101

Invalid token error when calling Microsoft graph from Web API

What I need to do is to calling Microsoft graph from C# Web API. This is basically what my method looks like:

var clientCredential = new ClientCredential(clientId, clientSecret);
AuthenticationContext context = new AuthenticationContext(authority);
AuthenticationResult authenticationResult = context.AcquireToken(clientId, clientCredential);
if (authenticationResult == null)
{
    throw new InvalidOperationException("Failed to obtain the token");
}
return authenticationResult;

what i receive by this call is:

enter image description here


then my idea is to call microsoft graph passing this token (and i'm not even 100% sure that this is the correct way of implementing the implicit flow)...but after that i receive the "invalid token" error.

could anyone tell me where i'm doing wrong here? Thank you alot!

Upvotes: 0

Views: 634

Answers (1)

juunas
juunas

Reputation: 58723

Your problem is the way you acquire the token.

This should be:

context.AcquireToken("https://graph.microsoft.com", clientCredential);

The first parameter is the resource URI for the API you want a token for.

Also this is not implicit flow, it is client credentials flow where the app accesses an API as itself, using application permissions.

Upvotes: 1

Related Questions