Free Support Forum - aspose.com

Aspose.Email, EWS with OAuth2 using a clientID + clientSecret

Hello,

I’m attempting to connect to an Office365 Mailbox using a .NET Windows Service application that’s a registered application with AzureAD and has appropriate permissions to the Exchange mailboxes in Office365. The application has a clientID and Client Secret.

I have implemented code using the MS ADAL to obtain the OAuth2 token based on the Client ID and Client Secret and I’m confident the token is valid, however when I construct the OAuthNetworkCredential with the token and pass that along to GetEWSClient(), I get an exception indicating “ExchangeImpersonation SOAP header must be present for this type of OAuth token.”

From documentation I’m looking at, it’s only possible to set Impersonation on the EWSClient after the call to GetEWSClient(). Is there a way to work around this or a better way to do what I’m trying to do?

Can the Aspose EWSClient work with OAuth2 tokens obtained via ClientID + Client Secret?

My sample code is abstracted below, assume the correct addresses and such for authority, resource, etc.:

AuthenticationContext authContext = new AuthenticationContext(authority, false);

ClientCredential clientCred = new ClientCredential(clientId, clientSecret);
var authResult = authContext.AcquireTokenAsync(resource, clientCred);

OAuthNetworkCredential oAuthCredential = new OAuthNetworkCredential([username], authResult.Result.AccessToken);

IEWSClient ewsClient = EWSClient.GetEWSClient(mailboxUri, oAuthCredential); //Impersonation exception

@devjosh,

I have observed your requirements and suggest you to please follow the thread below for your kind reference.