Outlook加载项REST调用错误

时间:2018-01-12 10:41:48

标签: outlook-addin office-addins outlook-restapi

我尝试使用Outlook rest API标记(标记)邮件,但我不断收到错误消息。我尝试使用不同的其他网址,但它没有帮助 - 错误只是变化。

清单中允许我相信的重要值是:

<Requirements>
    <Sets>
      <Set Name="Mailbox" MinVersion="1.1" />
    </Sets>
  </Requirements>
...
<Permissions>ReadWriteItem</Permissions>
  <Rule xsi:type="RuleCollection" Mode="Or">
    <Rule xsi:type="ItemIs" ItemType="Message" FormType="Read" />
  </Rule>
...
<VersionOverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides" xsi:type="VersionOverridesV1_0">
    <Requirements>
      <bt:Sets DefaultMinVersion="1.3">
        <bt:Set Name="Mailbox" />
      </bt:Sets>
    </Requirements>

以下是我试图做的部分导致错误:

Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (result)
{
    if (result.status === "succeeded")
    {
        var accessToken = result.value;
        var itemId = getItemRestId();
        var restUrl = Office.context.mailbox.restUrl + "/api/v2.0/messages/" + itemId;

        var request = {
            url: restUrl,
            type: "PATCH",
            dataType: 'json',
            data: { "Flag": { "FlagStatus": "Flagged" } },
            headers: { 
                "Authorization": "Bearer " + accessToken, 
                "Conntent-Type": "application/json" 
            }
        };

        $.ajax(request)
            .done(function (item)
            {
                // dome something
            })
            .fail(function (error)
            {
                // handle error
            });
    }
    else
    {
        // handle error
    }
});

function getItemRestId()
{
    if (Office.context.mailbox.diagnostics.hostName === 'OutlookIOS')
    {
        return Office.context.mailbox.item.itemId;
    }
    else
    {
        return Office.context.mailbox.convertToRestId(
            Office.context.mailbox.item.itemId,
            Office.MailboxEnums.RestVersion.Beta
        );
    }
}

上面的代码会导致错误:

{"readyState":4,"responseText":"","status":404,"statusText":"Not Found"}

如果我尝试JSON.stringify()我得到的请求的数据属性:

{"readyState":4,"responseText":"","status":404,"statusText":"Not Found"}

如果我将其余网址更改为(在较旧的示例中看到):

'https://outlook.office.com/api/beta/me/messages/'+ itemId;

请求的headers属性(在旧样本中看到):

headers: {
    'Authorization': 'Bearer ' + accessToken,
    'Content-Type': 'application/json'
}

然后我得到以下错误:

{
  "readyState": 4,
  "responseText": "{\"error\":{\"code\":\"ErrorAccessDenied\",\"message\":\"The api you are trying to access does not support item scoped OAuth.\"}}",
  "responseJSON": {
    "error": {
      "code": "ErrorAccessDenied",
      "message": "The api you are trying to access does not support item scoped OAuth."
    }
  },
  "status": 403,
  "statusText": "Forbidden"
}

有谁能看到我在这里做错了什么或错过了什么?

我在Outlook 2016中进行调试,帐户是Office 365。

更新:Fiddler输出

以下是我自己的示例发送的请求(结果为403 Forbidden) 确切错误: {&#34;错误&#34;:{&#34;代码&#34;:&#34; ErrorAccessDenied&#34;,&#34;消息&#34; :&#34;您尝试访问的API不支持项目范围的OAuth。&#34;}}

PATCH https://outlook.office.com/api/beta/me/messages/AAMkAGNmMDllMTVhLTI3ZDctNDYxZS05ZWM5LTA3ZWQzMzYyNDBiOABGAAAAAAD6OQOAoKyKT6R02yYFe0bIBwD5fUzv7OgQQYAILztCFSSWAALg591rAAC382lxTQ2HQpUKZsAGTeWVAARPu37CAAA= HTTP/1.1
Content-Type: application/json
Accept: application/json, text/javascript, */*; q=0.01
Authorization: Bearer <long token code removed...>
Referer: https://localhost:44394/MessageRead.html?_host_Info=Outlook$Win32$16.02$da-DK
Accept-Language: da-DK
Origin: https://localhost:44394
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: outlook.office.com
Content-Length: 33
Connection: Keep-Alive
Cache-Control: no-cache

{"Flag":{"FlagStatus":"Flagged"}}

以下是演示项目发送的请求(结果为200 OK)

PATCH https://outlook.office.com/api/beta/me/messages/AAMkAGNmMDllMTVhLTI3ZDctNDYxZS05ZWM5LTA3ZWQzMzYyNDBiOABGAAAAAAD6OQOAoKyKT6R02yYFe0bIBwD5fUzv7OgQQYAILztCFSSWAALg591rAAC382lxTQ2HQpUKZsAGTeWVAARPu37CAAA= HTTP/1.1
Content-Type: application/json
Accept: application/json, text/javascript, */*; q=0.01
Authorization: Bearer <long token code removed...>
Referer: https://<company.domain.com>:1443/outlookaddindemo/RestCaller/RestCaller.html?_host_Info=Outlook$Win32$16.02$da-DK
Accept-Language: da-DK
Origin: https://<company.domain.com>:1443
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: outlook.office.com
Content-Length: 47
Connection: Keep-Alive
Cache-Control: no-cache

{
  "Flag": {
    "FlagStatus": "Flagged"
  }
}

我能看到的唯一区别是,第二个请求有效负载似乎已经格式化为读取,而数据明显与前一个相同。

我似乎无法在这里找到问题 - 我甚至确保两个项目都使用相同版本的JQuery。

1 个答案:

答案 0 :(得分:1)

如果您需要通过REST对项目进行写入访问,则需要在清单中的ReadWriteMailbox元素中指定Permissions。尽管它的名称,ReadWriteItem没有给你一个具有适当范围的令牌。除ReadWriteMailbox之外的任何权限级别都会提供项目范围的标记,并且正如错误所示,您尝试执行的操作不支持项目范围的OAuth。

有关详细信息,请参阅https://docs.microsoft.com/en-us/outlook/add-ins/use-rest-api,但这里是相关位:

  

加载项权限和令牌范围

     

通过REST API考虑您的加载项需要什么级别的访问权限非常重要。在大多数情况下,getCallbackTokenAsync返回的令牌将仅提供对当前项的只读访问权限。即使您的加载项在其清单中指定了ReadWriteItem权限级别,也是如此。

     

如果您的加载项需要对当前项目或用户邮箱中的其他项目具有写入权限,则加载项必须在其清单中指定ReadWriteMailbox权限级别。在这种情况下,返回的令牌将包含对用户的消息,事件和联系人的读/写访问权。