Outlook 加载项 REST 调用错误
Outlook Add-in REST call error
我正在尝试使用 Outlook rest API 标记(标记)邮件,但我不断收到错误消息。我尝试了不同的休息 URLs 但它没有帮助 - 错误只是变化。
清单中允许这样做的重要值我认为是:
<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"}
如果我将其余的 URL 更改为(在旧示例中看到):
'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)
确切错误: {"error":{"code":"ErrorAccessDenied","message":"The api you are trying to access does not support item scoped OAuth."} }
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.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.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。
如果您需要通过 REST 对项目进行写访问,则需要在清单的 Permissions
元素中指定 ReadWriteMailbox
。尽管有它的名字,ReadWriteItem
并没有给你一个具有适当范围的标记。 ReadWriteMailbox
以外的任何权限级别都会提供项目范围的令牌,并且如错误所述,您尝试执行的操作不支持项目范围的 OAuth。
有关详细信息,请参阅 https://docs.microsoft.com/en-us/outlook/add-ins/use-rest-api,但这里是相关位:
Add-in permissions and token scope
It is important to consider what level of access your add-in will need via the REST APIs. In most cases, the token returned by getCallbackTokenAsync
will provide read-only access to the current item only. This is true even if your add-in specifies the ReadWriteItem
permission level in its manifest.
If your add-in will require write access to the current item or other items in the user's mailbox, your add-in must specify the ReadWriteMailbox
permission level in its manifest. In this case, the token returned will contain read/write access to the user's messages, events, and contacts.
我正在尝试使用 Outlook rest API 标记(标记)邮件,但我不断收到错误消息。我尝试了不同的休息 URLs 但它没有帮助 - 错误只是变化。
清单中允许这样做的重要值我认为是:
<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"}
如果我将其余的 URL 更改为(在旧示例中看到):
'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) 确切错误: {"error":{"code":"ErrorAccessDenied","message":"The api you are trying to access does not support item scoped OAuth."} }
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.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.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。
如果您需要通过 REST 对项目进行写访问,则需要在清单的 Permissions
元素中指定 ReadWriteMailbox
。尽管有它的名字,ReadWriteItem
并没有给你一个具有适当范围的标记。 ReadWriteMailbox
以外的任何权限级别都会提供项目范围的令牌,并且如错误所述,您尝试执行的操作不支持项目范围的 OAuth。
有关详细信息,请参阅 https://docs.microsoft.com/en-us/outlook/add-ins/use-rest-api,但这里是相关位:
Add-in permissions and token scope
It is important to consider what level of access your add-in will need via the REST APIs. In most cases, the token returned by
getCallbackTokenAsync
will provide read-only access to the current item only. This is true even if your add-in specifies theReadWriteItem
permission level in its manifest.If your add-in will require write access to the current item or other items in the user's mailbox, your add-in must specify the
ReadWriteMailbox
permission level in its manifest. In this case, the token returned will contain read/write access to the user's messages, events, and contacts.