我正在构建 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/