IMAP Authentication Failed with access token

See original GitHub issue

Hello, I am sorry for opening this issue but after I searched a lot I still couldn’t understand if what I am trying to do is possible or if I am doing it wrong.

I got an access token from the OAuth 2.0 authorization code flow (https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow). I know that until this step everything is fine because I can refresh the token with success.

I am trying to use that access token, in ImapClient.Authenticate(), but I get the “Authentication failed” message, as if I am using wrong credentials. I thought it would be possible to just have the token and use it in the Authenticate() method. Am I doing anything wrong or is it only possible to use the access token retrieved with the PublicClientApplication like in the example in this link (https://github.com/jstedfast/MailKit/blob/master/ExchangeOAuth2.md) ?

Example code that fails for me: (MailKit Version=2.10.0.0)

const string username = "example@hotmail.com";
            string accesstoken = "accesstoken"; (retrieved from the auth code flow successfully)

            using (var imapClient = new ImapClient(new ProtocolLogger("imap.log")))
            {
                imapClient.Connect("outlook.office365.com", 993, SecureSocketOptions.SslOnConnect);
                try
                {
                    imapClient.Authenticate(new SaslMechanismOAuth2(username, accesstoken));
                }
                catch (Exception ex)
                { 
                 // irrelevant in my case since I am running a testing app just for this
                }
            } 

StackTrace:

 at MailKit.Net.Imap.ImapClient.<AuthenticateAsync>d__81.MoveNext() in D:\src\MailKit\MailKit\Net\Imap\ImapClient.cs:line 850
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at MailKit.Net.Imap.ImapClient.Authenticate(SaslMechanism mechanism, CancellationToken cancellationToken) in D:\src\MailKit\MailKit\Net\Imap\ImapClient.cs:line 913

Protocol log:

Connected to imaps://outlook.office365.com:993/
S: * OK The Microsoft Exchange IMAP4 service is ready. [UABSADMAUAAxADkAMgBDAEEAMAAwADIANwAuAEUAVQBSAFAAMQA5ADIALgBQAFIATwBEAC4ATwBVAFQATABPAE8ASwAuAEMATwBNAA==]
C: A00000000 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
S: A00000000 OK CAPABILITY completed.
C: A00000001 AUTHENTICATE XOAUTH2 ${token}
S: A00000001 NO AUTHENTICATE failed.

Once again, I am sorry if there is already an answer for this and I couldn’t find it. Hope you can help. Thank you.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jstedfastcommented, Nov 23, 2020

My guess is that if it’s not working for you, then you probably aren’t getting the correct access_token.

I’m not sure what you could be doing wrong in getting the access_token you are getting, but I can confirm that the code I posted in the ExchangeOAuth2.md file works.

I would definitely recommend starting with the code I wrote in that guide.

0reactions
jstedfastcommented, Nov 24, 2020

Awesome! Glad it was something as simple as the scope!

Read more comments on GitHub >

github_iconTop Results From Across the Web

IMAP connection with oauth2 have been failing ...
All, i'm successfully getting access token, but unable to get authorized for IMAP server. the response is always authentication failed. Copy.
Read more >
Article: IMAP Connector to Office 365 using OAuth2.0 error ...
Cause. One possible reason for this issue is that the Access Tokens that were not created with the same user configured on the...
Read more >
Not able to authenticate office 365 via IMAP auth-2.0
I am trying to get all the emails sent to a email address into my node.js application. For this, I am using IMAP....
Read more >
Accessing guest outlook.office365.com mailbox via IMAP- ...
OAuth2- Accessing guest outlook.office365.com mailbox via IMAP- ... I do get the token but it throws "NO AUTHENTICATE FAILED" error.
Read more >
IMAP setup fails with AUTHENTICATE Failed error in logs ...
When trying to set up an IMAP service for JIRA applications notifications, you get an error AUTHENTICATE Failed .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found