office365 - 使用 Outlook 加载项 token 通过 EWS Java API 获取当前项目服务器端

标签 office365 outlook-addin office365api office-addins

我正在构建 Outlook 加载项,并尝试使用登录 token 通过 EWS Java API“下载”当前选定的项目(= 邮件消息)。下一步是获取附件并在我们的系统服务器端处理它们。

我正在关注dev doc我使用 mailbox.getCallbackTokenAsync 方法来检索登录 token 。 我将此 token 发布到我们的服务器(也为加载项提供服务)并使用 EWS Java Api 来获取当前选定的项目。

但我无法使用 token 登录。我在服务器端收到的异常是远程服务器返回错误:(401)未经授权

在添加 token 中,我使用以下 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 对象包含我在上面 javascript 的 header 中发布的信息):

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 ews-java-api 直接指向我的右边。我错误地使用了“TokenCredentials”,因为它不支持 OAuth 登录。 ews java api 有 not yet完全支持使用 OAuth token 登录。幸运的是,我们可以在请求中传递 http header ,所以这对我有用:

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

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

关于office365 - 使用 Outlook 加载项 token 通过 EWS Java API 获取当前项目服务器端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40745738/

相关文章:

c# - 通过 EWS API 连接到 Office 365

email - 发送邮件消息 : The SMTP server requires a secure connection or the client was not authenticated.

.net - Outlook 2003 和 Outlook 2007 插件 COM 资源发布

ssl - 我可以为 Office 365 outlook 插件加载混合内容 Http 和 Https 吗?

asp.net - invalid_scope 错误 AADSTS70011,为什么我收到此错误

office365 - Microsoft Graph API for Excel 是否可以公开访问?

SharePoint o365 自定义功能区操作

javascript - 如何在 Outlook 插件集成中获取用户的实际电子邮件 ID

multithreading - 如何在 Outlook 2007 加载项中高效运行后台任务?

excel - 如何在 Sharepoint 框架 (SPFx) 应用程序中使用 Excel Rest API 访问存储在 Sharepoint 在线文档库中的 Excel 数据?