Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EWS java apis using Oauth2

I want to use Oauth2 authentication for my app. I want to use EWS Java apis to fetch data from O365. Is it possible? Document http://blogs.msdn.com/b/exchangedev/archive/2014/09/24/10510847.aspx talks about fetching oauth token for REST apis should I use same document to fetch token to be used with EWS web services also? Can anyone share any code sample doing this with java.

like image 207
pranjaljain Avatar asked Feb 10 '15 05:02

pranjaljain


2 Answers

I know, the question is quite old but the answers & comments still helped me out today. So, I want to briefly wrap it up:

In this pull-request: https://github.com/OfficeDev/ews-java-api/pull/321 the header validation was removed as described in the comments of the accepted answer.

So, its enough to set the token via

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.getHttpHeaders().put("Authorization", "Bearer " + officeToken);

Don't set any additional credentials.

For completeness: In my scenario, the officeToken is retrieved on client side via the Office JavaScript api

Office.initialize = function() {
    $(document).ready(function (){
        Office.context.mailbox.getCallbackTokenAsync(function(result) {
            result.value; // is the officeToken of above
            // do s.th. with the officeToken; e.g. send it to the server
        });
    });
});

On the server, we can now get the content of a mail. In the newest version of the Office JavaScript Api, this is also possible directly in the client. However, your Exchange Api version must be 1.3. So, this solution with retrieving the token and sending it to a server is useful in case your Exchange server runs an older version.

like image 184
Benjamin Raethlein Avatar answered Nov 11 '22 01:11

Benjamin Raethlein


It is possible. You have to register your app in the same way as you do for REST, but you need to specify the special EWS permission "Have full access via EWS to users' mailboxes". You'll need to do the OAuth flow to retrieve the access token, then include that in the Authorization header in your EWS requests. I don't have a Java sample for you, but those are the basic steps required.

like image 5
Jason Johnston Avatar answered Nov 11 '22 01:11

Jason Johnston