Error for Outlook Oauth2

0 votes
asked Nov 16, 2016 by MrinalGrover (200 points)

OAuth20 authentication is failing for Gmail.

Below is the smtpException I am getting:
Rebex.Net.SmtpException: OAuth failed: OAuth authentication failed due to Invalid token. Code -2147184118

For gmail, I'd given full scope - mail.google.com/ but nothing is mentioned for outlook. This is what I've given as scope for outlook - openid+offline_access+profile+https:%2f%2foutlook.office.com%2fmail.send

Below is the code snippet:

try{
        Smtp client = new Smtp();
        client.Connect("smtp-mail.outlook.com", SslMode.Explicit);

        // prepare token
        string pattern = string.Format("user={0}{1}auth=Bearer {2}{1}{1}", userEmailId, '\x1', accessToken);
        string token = Convert.ToBase64String(Encoding.ASCII.GetBytes(pattern));

        client.Login(token, SmtpAuthentication.OAuth20);
    }
    catch(SmtpException ex)
    {}
}

Kindly let me know if there is anything that I'm doing wrong here. I could make this work with Gmail. I've also tried to use IMAP instead of SMTP for outlook but no result.

Regards
Mrinal

Applies to: Rebex Secure Mail

1 Answer

0 votes
answered Nov 16, 2016 by Lukas Matyska (36,240 points)
selected Nov 21, 2016 by MrinalGrover
 
Best answer

We wrote a blopost about Gmail OAuth 2.0 authentication.

It contains sample application you can try.

For Outlook 365 I found this answer on StackOverflow:

We don't support OAuth for IMAP access to Office 365 ... OAuth is supported for Office 365 REST APIs ... and our older SOAP APIs Exchange Web Services.

It seems the OAuth for SMTP is not supported as well. However OAuth for EWS is supported. You can use Rebex.Net.Ews class in this case.

And the last thing. In OAuth for EWS we are successfully using the access token directly (without any processing). I suggest you to try it as well in case of reported invalid token error. So, your code can look like this:

//client.Login(token, SmtpAuthentication.OAuth20);
client.Login(accessToken, SmtpAuthentication.OAuth20);
commented Nov 17, 2016 by MrinalGrover (200 points)
Tried this for EWS Client. It seems there's no Login method for (accessToken, Authentication). Can you please verify?
commented Nov 17, 2016 by Tomas Knopp (58,560 points)
edited Dec 20, 2016 by Tomas Knopp
Hi, you are right, the OAuth support for our EWS client is going to be released as part of the next version of Rebex components.
commented Dec 20, 2016 by Tomas Knopp (58,560 points)
Hi, the OAuth support in EWS client has been released as part of Rebex Secure Mail 2016 R3 version.
...