ExchangeImpersonation SOAP header must be present for this type of OAuth token

I am implementing OAuth2 for EWS. After getting the token when I make a request for connection to mail box it is throwing the below error.

“ExchangeImpersonation SOAP header must be present for this type of OAuth token.”

For impersonating the user I need to instantiate IEWSClient before connecting.

Aspose version : 20.9.1
Language : C#

Hi Team,

Any update on this issue.

@suryamohan05

Please upgrade to the latest version of Aspose.Email for .NET 22.8 and let us know how it goes on your side.

We suggest you please read the following thread to implement OAuth2 for EwsClient. Hope this helps you.

@suryamohan05

If you are not able to download the code from other forum thread, please download the code examples from here:
EWSModernAuthenticationApp.zip (3.4 KB)
EWSModernAuthenticationImapSmtp.zip (3.9 KB)
EWSModernAuthenticationDelegated.zip (3.6 KB)

A post was split to a new topic: Modern Authentication code examples

That way it works

var cca = ConfidentialClientApplicationBuilder
			.Create("eXXXXXXX-xxxx-xxxx-xxxx-xxxxxxxxxxxx")     
			.WithClientSecret("xxxxx~xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")     
			.WithTenantId("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx^)     
			.Build();

var appScopes = new string[] { "https://outlook.office365.com/.default" };
   
//get token with App Auth
var authResult = await cca.AcquireTokenForClient(appScopes).ExecuteAsync();

// Configure the ExchangeService with the access token
var ewsClient = new ExchangeService();
ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "max.mustermann@xxxxxxxxxxxxxxx.de");

But when I try to use the Aspose EWSClient I run into this error, as soon as I call “GetEWSClient” →
ExchangeImpersonation SOAP header must be present for this type of OAuth token

Here the part that doesn´t work

var cca = ConfidentialClientApplicationBuilder
			.Create("eXXXXXXX-xxxx-xxxx-xxxx-xxxxxxxxxxxx")     
			.WithClientSecret("xxxxx~xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")     
			.WithTenantId("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx^)     
			.Build();

var appScopes = new string[] { "https://outlook.office365.com/.default" };
var authResult = cca.AcquireTokenForClient(appScopes).ExecuteAsync().GetAwaiter().GetResult();
NetworkCredential cred = new OAuthNetworkCredential(authResult.AccessToken);
	
var ewsClient = EWSClient.GetEWSClient("https://outlook.office365.com/EWS/Exchange.asmx", cred);

Where do I have do call the ImpersonateUser method?
Aspose.Email 23.7.0 is used.
Language: C#

Thanks for help.

Hello @JSprock,

Thanks for contacting us.
Please try to use the following OAuthNetworkCredential constructor:

NetworkCredential credentials = new OAuthNetworkCredential("max.mustermann@xxxxxxxxxxxxxxx.de", authResult.AccessToken);

Hi margarita.samodurova,

that do the trick! Problem solved.

Totaly awsome, thanks for your quick response and help!
Have a nice day.

grettings
Jörg

Hello @JSprock ,

That’s great! If you have any further questions, feel free to ask.