Outlook JS加载项:来自令牌的凭据(本地交换,不是OAuth)

时间:2019-07-19 15:38:22

标签: service outlook exchange-server exchangewebservices office-js

由于Office API中消息大小的限制,我试图构建一种服务:

  • Office客户端获取身份令牌(getCallbackTokenAsync)
  • 向服务发布包含身份令牌的消息
  • 服务使用新的令牌凭据创建新的新Exchange服务

限制:

  • EWS服务器是内部安装的,当前无法使用OAuth
  • 我当前正在将其用作RESTful服务

我尝试过getUserIdentityToken,但这对凭据没有用。

Office JS调用

var _appName;
var _mailbox;
var _emailAddress;
var _itemId;
var _identityToken;
var _ewsUrl;

    app.initialize();
    _mailbox = Office.context.mailbox;
    var item = _mailbox.item;
    _itemId = item.itemId;
    _ewsUrl = _mailbox.ewsUrl;
    _emailAddress = _mailbox.userProfile.emailAddress;

    function MakeRequest(serviceUrl, callType, format) {

        _mailbox.getCallbackTokenAsync(function (asyncResult) {
            if (asyncResult.error) {
                app.showNotification(_appName, "Error getting Use Identity Token: " + asyncResult.error.message);
            }
            else {

                _identityToken = asyncResult.value;

                var correctedItemId = _itemId.replace("+", "_");
                var request = { ItemId: correctedItemId, UserEmailAddress: _emailAddress, UserIdentityToken: _identityToken, EwsUrl: _ewsUrl, AudienceUrl: _audUrl, ReturndNewId: false };

                $.ajax({
                    crossDomain: true,
                    url: serviceUrl,
                    type: callType,
                    data: JSON.stringify(request),
                    contentType: 'application/json; charset=utf-8',
                    dataType: format,
                    success: function (asyncResultSuccess) {
                        LogResult(asyncResultSuccess.statusText);
                        //app.showNotification(_appName, asyncResultSuccess.statusText);
                    },
                    error: function (asyncResultError) {
                        LogResult(asyncResultError.statusText);
                        //app.showNotification(_appName, asyncResultError.statusText);
                    }
                });
            }
        });
    }

服务器端凭证:

    void LoadSimple()
    {
        try
        {
            service = new ExchangeService(ExchangeVersion.Exchange2013_SP1)
            {
                Credentials = new TokenCredentials(RequestProperties.UserIdentityToekn),
                Url = new Uri(RequestProperties.EwsUrl)
            };
            ReturnProps = new EwsReturnItem();
        }
        catch (Exception ex)
        {
            log.Error(string.Format("{0}: {1}", MethodBase.GetCurrentMethod().Name, ex.Message));
        }
    }

服务器端转发消息:

    public EwsReturnItem Forward(string toRecipient, string ccRecipient, string subjectPrepend, string bodyText)
    {
        try
        {
            var correctedId = CorrectItemId(RequestProperties.ItemId);
            EmailMessage email = EmailMessage.Bind(service, correctedId, new PropertySet(ItemSchema.MimeContent, ItemSchema.Subject));
            ResponseMessage forwardMessage = email.CreateForward();
            forwardMessage.Subject = String.Format("{0}{1}", subjectPrepend, email.Subject);
            forwardMessage.ToRecipients.Add(toRecipient);
            forwardMessage.CcRecipients.Add(ccRecipient);
            forwardMessage.BodyPrefix = bodyText;

            ...snip


        }
        catch (Exception ex)
        {
            log.Error(string.Format("{0}: {1}", MethodBase.GetCurrentMethod().Name, ex.Message));
            return new EwsReturnItem() { StatusText = ex.Message };
        }
    }

此行由于未授权而失败:

EmailMessage email = EmailMessage.Bind(service, correctedId, new PropertySet(ItemSchema.MimeContent, ItemSchema.Subject));

0 个答案:

没有答案