Error Access Denied. Check your credentials in Graph API in console application

Diya Rawat 6 Reputation points
2021-08-13T04:14:45.257+00:00

Trying to retrieve the emails details (EmailTo, EmailFrom, CC, BCC, EmailBody, EmailSent, etc). I have register the app from azure portal, selected for console app
122957-image.png

Given The API Permission

122857-image.png

public async static Task getUsersAsync()
{
var clientId = "XXXX";
var tenantId = "XXXX";
var clientSecret = "XXXX"; //Client Secret Value

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantId)
.WithClientSecret(clientSecret)
.Build();

ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);

//var msz = await graphClient.Me.MailFolders.Inbox.Request().GetAsync();

var messages = await graphClient.Me.Messages
.Request()
.Select("sender,subject")
.GetAsync();
//var groups = await graphClient.Groups.Request().Select(x => new { x.Id, x.DisplayName }).GetAsync();
foreach (var group in messages)
{
Console.WriteLine($"{group.Body}, {group.Id}");
}
}

When i am trying to run the application, it is throwing error
122858-image.png

Microsoft Security Microsoft Entra Microsoft Entra ID
Microsoft Security Microsoft Graph
Developer technologies .NET .NET CLI
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. JanardhanaVedham-MSFT 3,566 Reputation points
    2022-03-15T13:02:17.96+00:00

    Hi @Diya Rawat ,

    Sorry for the delayed reply.

    You are receiving this error becuase /me/messages Microsoft Graph endpoint or API works with delegated pemissions scope only. As per the screenshot shared above, I do not see either "Mail.Read" or "Mail.ReadBasic" delegated permissions are granted to your app and make sure to grant these delegated permissions to your app to resolve the issue.

    https://graph.microsoft.com/v1.0/me/messages?$select=sender,subject  
    

    Example:

    183264-image.png

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.