Hi @JainRajendraKumarHarshith-5881
The outlook api is obviously wrong as the token audience, if you call the graph api with that token you will get a 401 error.
As you said, you need the graph api as the audience for the token. The solution is to change the scope to graph api.
I have written a piece of code for getting token for graph api, it should help you, please refer to:
var scopes = new[] { "https://graph.microsoft.com/.default" };
// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = "{tenant id}";
// Value from app registration
var clientId = "{client id}";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var userName = "{user name}";
var password = "{password}";
// https://learn.microsoft.com/dotnet/api/azure.identity.usernamepasswordcredential
var userNamePasswordCredential = new UsernamePasswordCredential(
userName, password, tenantId, clientId, options);
var accessToken = await userNamePasswordCredential.GetTokenAsync(new TokenRequestContext(scopes) { });
Console.WriteLine(accessToken.Token);
Parse the token.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.