1

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

cbx
  • 11
  • 2
  • Are you sure the account you use to sign into Graph Explorer are the same as the one used in UsernamePasswordProvider? Is it a work account (O365 or AAD account) or personal account ([MSA](https://account.microsoft.com/account?lang=en-us))? – Allen Wu Dec 28 '20 at 02:38
  • The same O365 account used is tester@cortex8.onmicrosoft.com – cbx Dec 30 '20 at 06:47
  • Can you access the emails of this mailbox using Microsoft Graph explorer or still you're getting the same error? – Dev Jan 04 '21 at 11:46
  • I'm facing the same issue. I tried many ways to get access: username/password, device code provider. Every time, I'm performing the following code:
    `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

0 Answers0