Can successfully get user's emails by using Graph Explorer or DeviceCodeProvider. DeviceCodeProvider
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create(clientId)
.Build();
Func<DeviceCodeResult, Task> deviceCodeReadyCallback = async dcr => await Console.Out.WriteLineAsync(dcr.Message);
DeviceCodeProvider authProvider = new DeviceCodeProvider(publicClientApplication, scopes, deviceCodeReadyCallback);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var messages = await graphClient.Me.Messages
.Request()
.Select("sender,subject")
.GetAsync();
Failed to get the same user's emails when used PublicClientApplicationBuilder with UsernamePasswordProvider or ConfidentialClientApplicationBuilder. Graph API return
Exception has occurred: CLR/Microsoft.Graph.ServiceException An exception of type 'Microsoft.Graph.ServiceException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Code: MailboxNotEnabledForRESTAPI Message: REST API is not yet supported for this mailbox.
UsernamePasswordProvider
IPublicClientApplication clientApp = PublicClientApplicationBuilder.Create(clientId)
.WithAuthority(AzureCloudInstance.AzurePublic, tenantId)
.Build();
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(clientApp, scopes);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
String password = userPassword;
System.Security.SecureString sec_pass = new System.Security.SecureString();
Array.ForEach(password.ToArray(), sec_pass.AppendChar);
sec_pass.MakeReadOnly();
var me = await graphClient.Me.Messages.Request()
.WithUsernamePassword(userPrincipalName, sec_pass)
.Select("sender,subject")
.GetAsync();
ConfidentialClientApplicationBuilder
var scopes = new[] { "https://graph.microsoft.com/.default" };
var authority = $"https://login.microsoftonline.com/{tenantId}";
var app = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(new Uri(authority))
.Build();
var authenticationProvider = new ClientCredentialProvider(app);
var graphClient = new GraphServiceClient(authenticationProvider);
var user = await graphClient.Users[userPrincipalName].Request().GetAsync();
var mailFolders = await graphClient.Users[user.Id].Messages.Request().GetAsync();
The service built is running in back-end. Because of this, the popup login must be avoided. What did I do wrong? please help. Thanks in advance
`await graphClient.Me.Events.Request().GetAsync();`
I'm facing this issue.
I've also tried to get application access. In this case I only see Access denied error. But through the Graph Explorer everything works perfectly. So it should be a configuration mismatch. – Serdg D. Jan 25 '21 at 09:16