ews-java-api icon indicating copy to clipboard operation
ews-java-api copied to clipboard

OAuth with EWS Java APIs

Open nshowkath opened this issue 11 years ago • 18 comments

Hi ,

We found there is Limitation in EWS Java APIs: EWS Java APIs that we download from https://github.com/OfficeDev/ews-java-api only Support EWS features till Exchange2010_SP2, it does have any special classes to pass OAuth token. EWS Managed APIs for .net has the Microsoft.Exchange.WebServices.Data.OAuthCredentials class through which we can pass OAuth token but till now EWS Managed Java APIs has not provided that class.

So, we need to pass OAuth token as authorization header. Also, when I tried passing “Bearer” type OAuth token as authorization header, service.getHttpHeaders().put("Authorization", "Bearer "); but it gave me error saying "The request failed. HTTP header 'Authorization=Bearer ' isn't permitted. Only HTTP headers with the 'X-' prefix are permitted." How this limitation was resolved? Because EWS Java API code is open source, We made some changes in the code, so they are now allowing OAuth bearer tokens as authorization header. We simply commented out the validate() method code in ExchangeServiceBase.java class, which was doing check for HTTP headers to be with ‘X-’ prefix and was not allowing ‘bearer’ type token to pass. Now, we can pass OAuth token as,

service.getHttpHeaders().put("Authorization", "Bearer ");

Could someone from you team make this changes or you can suggest any other solution.

Thanks & Regards, Showkath.

nshowkath avatar Apr 07 '15 09:04 nshowkath

@nshowkath thanks for the given input. Maybe just commenting out the validation process might not be the best solution for this problem. At some point we should see differences between the ews-managed-api and ews-java-api and show up the points we should have implemented in both of them (regarding "costs" and the timeline).

serious6 avatar Apr 07 '15 20:04 serious6

Hi,

Thanks for your reply , could you let me know when can we expect this change in EWS JAVA API

Thanks, Showkath.

nshowkath avatar Apr 16 '15 12:04 nshowkath

@nshowkath see #288 for details. But this will depend on the number of PR coming in.

serious6 avatar Apr 29 '15 14:04 serious6

Thanks for your reply and considering it as enchantment . It would be great if your team get it done at earliest and give high priority to this requirement , otherwise we need to include your source code as part of our solution, which will causes future problem when your team implemented any new feature. Please let me know if you need more inputs on this issue or any support from me.

Thanks, Showkath.

nshowkath avatar Apr 30 '15 09:04 nshowkath

I did look into this and noticed that when this validation was added it was mainly motivated by the need to add an X- header. I also checked ews-managed-api latest code of ExchangeServiceBase no longer has this check. Digging into the history, the check was removed on 2012-MAR-20.

So I suggest removing the check within the validate method to be consistent with the EWS Managed API. This enables adding any headers including the authorization header and hence unblocking the OAuth scenario.

Then later, we can still add OAuth support as a first class feature so that users of the library don't need to provide custom headers. This will also bring ews-java-api to parity with ews-managed-api.

@serious6 do you have concerns about such approach? @nshowkath will this change address your concern?

vboctor avatar May 08 '15 00:05 vboctor

sounds good to me :+1:

serious6 avatar May 13 '15 07:05 serious6

Yes , this will resolve issue, could you let me know when can we expect this change in EWS JAVA API ?

Thanks, Showkath.

nshowkath avatar May 14 '15 07:05 nshowkath

Hi ! I need to authenticate to my EWS through a LtpaToken2 cookie .. is that possible? My web application is running under a WebSphere that haves a Single-Sign-On configuration (SPNEGO) .. So the credentials are negotiated between the browser - the websphere - and the active directory ... and when the user is authenticated a LtpaToken2 cookie is set into the response

Is that possible ?? Thanks !

acaride avatar Dec 16 '15 12:12 acaride

Hi All.. How to use the java ews api to authenticate an user?? Please provide me a java code. I have been looking for this every where and ended up asking here. Please reply at the earliest.

girish-kumar-710 avatar May 24 '16 10:05 girish-kumar-710

Hi @girish-kumar-710 have you found any java code for office365 oauth using EWS or any api? If so, can u plz share with me. Thanks.

agileravi avatar Dec 27 '16 09:12 agileravi

I can not find "office 365 exchange online" on add application in Azure Active Directory when register a new application to use oauth. And only find Microsoft Graph Permissions on the new Application Registration Porta(https://apps.dev.microsoft.com). Could you tell me where can I register my app to use oauth with ews-java-api ? @serious6

wulangbnu avatar Jan 21 '17 00:01 wulangbnu

I am not sure if the change fixed it. I have the app-only token I got from graph.microsoft.com using the client-credentials grant flow and seems like I cannot use that for EWS API? I was able to validate that the token is passed via the wire with the Authorization header set with the token but I still get 401 unauthorized.

Does EWS even support access tokens received from client-credentials grant flow? Please help.

keshi87 avatar Mar 14 '17 21:03 keshi87

Can anyone please tell me. Is this enough the below code will validate our user validation?

service.getHttpHeaders().put("Authorization", "Bearer ");

ShanavazJs avatar May 29 '18 13:05 ShanavazJs

Yes. It will work fine. Give as like this. Generate the Access token via Oauth2.

service.getHttpHeaders().put("Authorization", "Bearer " + accessToken);

samrajcse avatar May 29 '18 14:05 samrajcse

Thank you @samrajcse . I have passed the username and password for a validation it is working fine. without password will it be possible to validate the EWS by using user access token. If the user access token is enough to validate please tell me the procedure how to get an access token?

Please help me on that.

ShanavazJs avatar Jul 19 '18 09:07 ShanavazJs

You can follow the below steps to get the access token. We need to have Oath2 flow to generate tokens.

https://blogs.msdn.microsoft.com/exchangedev/2014/03/25/using-oauth2-to-access-calendar-contact-and-mail-api-in-office-365-exchange-online/

Regards, SAMRAJ.K

On Thu, Jul 19, 2018 at 3:09 PM ShanavazJs [email protected] wrote:

Thank you @samrajcse https://github.com/samrajcse . I have passed the username and password for a validation it is working fine. without password will it be possible to validate the EWS by using user access token. If the user access token is enough to validate please tell me the procedure how to get an access token?

Please help me on that.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/OfficeDev/ews-java-api/issues/284#issuecomment-406217742, or mute the thread https://github.com/notifications/unsubscribe-auth/AAqinNvml2gz1C2FpsINLVO12ZfOsTsjks5uIFO-gaJpZM4D7eam .

samrajcse avatar Jul 19 '18 09:07 samrajcse

when im trying to call ews api using access token it giving error :microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. The remote server returned an error: (401)Unauthorized

kirit7037 avatar Jul 18 '19 12:07 kirit7037

To avoid getting unauthorized pass scope : https://outlook.office.com/.default not https://graph.microsoft.com/.default

victormarcus avatar Feb 25 '20 12:02 victormarcus