+2 votes

I am testing the downloaded trail library on fetching outlook emails using Oauth2.0. I have successfully obtained refresh and access tokens using registered app. But when I use the sample code, shown below, to authenticate the IMAP connection, I got authentication error, "[AUTHENTICATIONFAILED] OAuth authentication failed.".

Can someone help? Thanks!

using (Imap imap = new Imap())
   imap.LoginOAUTH2(emailAddress, accessToken);  // cause error
   List<long> uids = imap.Search(Flag.All);
by (500 points)
edited by

1 Answer

0 votes
Best answer

You can find step-by-step instructions for OAuth web scenario here:

Make sure "Redirect URLs" is valid url. You can't use localhost. For testing purposes you can use a fake domain. But you must modify your computer's hosts file (c:\Windows\System32\drivers\etc\hosts) so it redirects to localhost (and to your web app): fake-domain-9065436322.com

Make also sure that ASP.NET Session ID does not change between requests. Just add anything to session and SessionId should stay the same for the user:

Session["UserIsHere"] = true;

For installed application OAuth scenario go here:

In both cases remember to add OutlookScope.ImapAndSmtp.Name and OutlookScope.EmailAddress.Name scopes.

by (298k points)
edited by
I might know the reason. I forgot to add the scope of "wl.imap".
Have you specified OutlookScope.ImapAndSmtp.Name and OutlookScope.EmailAddress.Name scopes? Does OutlookApi.GetEmail method return correct value?
Yeah. I added the scope of "wl.imap", and it worked now. I should have been more careful in reading the document.

Thanks a lot for your help!