使用 Outlook add-in 令牌通过 EWS Java API 获取当前项目服务器端

Use Outlook add-in token to get the current Item serverside with EWS Java API

我正在构建一个 Outlook add-in,我正在尝试 'download' 使用 EWS Java API 当前选定的项目(= 邮件消息)登录令牌。下一步是获取附件并在我们的系统服务器端处理它们。

我按照 dev doc 使用 mailbox.getCallbackTokenAsync 方法检索登录令牌。 我将此令牌发布到我们的服务器(也为 add-in 提供服务)并使用 EWS Java Api 来获取当前选定的项目。

但是我无法使用令牌登录。我在服务器端收到的异常是 The remote server returned an error: (401)Unauthorized

在添加令牌中,我使用以下 javascript 代码来调用我们的服务:

(function(){
  'use strict';

  // The Office initialize function must be run each time a new page is loaded
  Office.initialize = function(reason){
    jQuery(document).ready(function(){

        getAccessToken();
    });
  };


  // Retrieves an acccess token
  function getAccessToken(){
      Office.context.mailbox.getCallbackTokenAsync(exchangeTokenCallback);
  }

  function exchangeTokenCallback(asyncResult, userContext) {
        if (asyncResult.status === "succeeded") {

            // get info about selected mail message...
            var item = Office.cast.item.toItemRead(Office.context.mailbox.item);
            var email = Office.context.mailbox.userProfile.emailAddress;
            var loginToken = asyncResult.value;


            $.ajax({
                  headers: {"X-Outlook-Token-For-EWS":loginToken,
                            "X-Outlook-EwsUrl":Office.context.mailbox.ewsUrl,
                            "X-Outlook-ItemId": item.itemId
                  },
                  url: "/ac/api/email/ews"
                }).done(function(result) {

                    jQuery('#result').text(JSON.stringify(result));

                }).error(function(result) {

                    jQuery('#result').text(JSON.stringify(result));

                });

        } else {
            showToast("Error", "Could not get callback token: " + asyncResult.error.message);
        }
    };


})();

并且该服务使用此 Java 代码来获取项目(params object 包含我在 [=40] 中的 headers 中发布的信息=] 以上):

package nl.c2c.ac.api.service.email.outlookaddin;

import java.net.URI;

import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.PropertySet;
import microsoft.exchange.webservices.data.core.service.item.Item;
import microsoft.exchange.webservices.data.credential.TokenCredentials;
import microsoft.exchange.webservices.data.property.complex.ItemId;
import nl.c2c.ac.exceptions.CustomException;

public class EmailOutlookAddinService {

    private EmailOutlookAddinParams params;

    public EmailOutlookAddinService(EmailOutlookAddinParams params){
        setParams(params);
    }

    public String retrieveMessage() throws Exception{
        ExchangeService service = null;
        try {


            TokenCredentials credentials = new TokenCredentials(params.getAccessToken());
            service = new ExchangeService();  
            service.setCredentials(credentials);
            service.setUrl(new URI(params.getEwsUrl()));  //new URI("https://outlook.office365.com/EWS/Exchange.asmx")


            Item itm = service.bindToItem(new ItemId(params.getItemId()), PropertySet.getIdOnly());
            return "Subject: " + itm.getSubject();

        }catch(Throwable e){
            throw new CustomException(e);
        } finally {
            if(service!=null){
                service.close();
            }
        }


    }

    private void setParams(EmailOutlookAddinParams params) {
        this.params = params;
    }
}

我希望有更多经验的人能给我指出正确的方向。

我发现 issue the ews-java-api that pointed me in the right directly. I was incorrectly using the 'TokenCredentials' because it does not support OAuth logins. The ews java api has not yet 完全支持使用 OAuth 令牌登录。幸运的是我们可以在请求中传递 http headers,所以这对我有用:

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

除此之外,我不再需要使用 setCredentials 方法设置凭据。