Rbex.Ews, Invalid access token error

0 votes
asked Apr 13 by santoshgh (210 points)

Since Microsoft have moved outlook/live/hotmail accounts to outlook.office365.com, using EWS service to send the mail..

I have created the WebApp and registered it
https://apps.dev.microsoft.com

Using the end points for the Authorization
https://login.microsoftonline.com/common/oauth2/v2.0/authorize

Using the end point to get access token
https://login.microsoftonline.com/common/oauth2/v2.0/token

Request the below scopes as the part of authorization
scope=openid+offline_access+profile+https:%2f%2foutlook.office.com%2fmail.readwrite+https:%2f%2foutlook.office.com%2fmail.send

Since EWS required the full scope as per the msdn.

Connecting to EWS using access token getting error:

var ewsAuth = new Rebex.Net.Ews();
            ewsAuth.Connect("outlook.office365.com");
            string pattern2 = string.Format("user={0}{1}auth=Bearer {2}{1}{1}", creds.Email, '\x1', creds.AccessToken);
            string token2 = Convert.ToBase64String(Encoding.ASCII.GetBytes(pattern2));

//invalid token error
ewsAuth.Login(creds.AccessToken, EwsAuthentication.OAuth20);

            ewsAuth.SendMessage("john@example.org", "Test 1", "Hello world!");
Applies to: Rebex Secure Mail

1 Answer

0 votes
answered Apr 13 by Pavel Matyska (9,640 points)

Hi,

please follow the instruction as described on this MSDN page. It seems that you missed one step. The authorization token you got from the Authorization end point needs to be traded for the actual OAuth token, see chapter "Step Three: Trade-In for an access/refresh token" in provided MSDN page.

Also, we "discovered" that Google OAuth token format is very different from the one Microsoft uses (that format you are using resembles the google format to me, so it may be also an issue here). We used Microsoft’s libraries Microsoft.IdentityModel.Clients.ActiveDirectory to obtain correct OAuth token for us. The libraries can be found in NuGet repository.

...