0 votes
by (370 points)

Hi

I can successfully log into outlook.office365 using Rebex.Net.Imap. Logging in works and all related functions. However, I now want to use Rebex.Net.Ews to also send emails using OAuth. When I try to log in I get the following error: "OAuth token is invalid (invalid_token)."

I use the same server and token for both. Imap works, Ews does not.

This is my code:

Dim Token As String = GetAccessToken(Settings)
Dim Ews As Ews = New Ews()
Ews.Connect(Settings.ServerAddress, SslMode.Implicit)
Ews.Login(Token, EwsAuthentication.OAuth20)

Can you please give me any guidance why the Ews fails to log in.

Applies to: Rebex Secure Mail

1 Answer

0 votes
by (148k points)
selected by
 
Best answer

There is a slight difference between IMAP/POP3/SMTP and EWS. Unlike the other protocols, the OAuth2 token for EWS is not supposed to be wrapped in "user="/"auth=Bearer" structure along with the user name and then Base64-encoded. Instead, it's just passed to EWS in the original form.

In practical terms, this means that for IMAP, you are probably doing something like this:

// get OAuth 2.0 token
string accessToken = ...;

// prepare token format suitable for IMAP, POP3, or SMTP
string pattern = string.Format("user={0}{1}auth=Bearer {2}{1}{1}", userName, '\x1', accessToken);
string token = Convert.ToBase64String(Encoding.ASCII.GetBytes(pattern));

// authenticate using IMAP
imapClient.Login(token, ImapAuthentication.OAuth20);

But for EWS, you only have to pass the token to the Login method:

// get OAuth 2.0 token
string accessToken = ...;

// authenticate using EWS
ewsClient.Login(accessToken, EwsAuthentication.OAuth20);

Check out the following sample apps for details:

by (370 points)
Solved. Exactly what I was looking for. Thank you Lukas
...