Office 365 日历 API 使用 EWS 和 OAuth 2
Office 365 Calendar API Using EWS and OAuth 2
请查看底部的编辑内容。
我正在尝试使用 EWS(不是托管 API)与 Office 365 API 一起处理用户日历上的 create/delete/update 事件。
到目前为止,我已经成功地使用 Basic Auth 来验证我的 SOAP 请求是否有效。我现在正尝试用 OAuth 2 替换 Basic Auth。我需要使用客户端凭证流。
以下是我遵循的步骤:
已向应用程序提供管理员同意。我在浏览器中启动了以下 URL,并使用管理员帐户提供了同意。
https://login.microsoftonline.com/common/oauth2/authorize?
response_type=code+id_token&
scope=openid&
client_id=[Client ID]&
redirect_uri=http://localhost/myapp/permissions&
resource=https://outlook.office.com&
prompt=admin_consent&
response_mode=form_post&
nonce=1234
同意后,我从响应中检索了 id_token
,并使用 JWT.io 对其进行了解码。从有效负载中,我记录了 tid
.
接下来我通过发送以下请求检索了访问令牌:
POST https://login.microsoftonline.com/[TID]/oauth2/token HTTP/1.1
cache-control: no-cache
Content-Type: application/x-www-form-urlencoded
Accept: */*
Host: login.microsoftonline.com
accept-encoding: gzip, deflate
Connection: close
client_id=[CLIENT ID]&
client_secret=[CLIENT SECRET]&
grant_type=client_credentials&
resource=https%3A%2F%2Foutlook.office.com
使用访问令牌,我发出了与使用 Basic Auth 相同的请求,只是我将 Basic Auth header 替换为 Authorization: Bearer [Access Token]
我收到以下错误 (403 Forbidden
):
The token contains not enough scope to make this call.
我需要做什么来修复这个错误?
编辑 1: 我添加了 Use Exchange Web Services with full access to all mailboxes
应用程序权限,现在发送 SOAP 消息会导致 500 Internal Server Error
...
解决方案涉及以下内容:
添加 Use Exchange Web Services with full access to all mailboxes
应用程序权限,因为 EWS 不允许使用更精细的权限。
正在为目标邮箱添加一个ExchangeImpersonation SOAP header。
正在设置 X-AnchorMailbox HTTP header。
请查看底部的编辑内容。
我正在尝试使用 EWS(不是托管 API)与 Office 365 API 一起处理用户日历上的 create/delete/update 事件。
到目前为止,我已经成功地使用 Basic Auth 来验证我的 SOAP 请求是否有效。我现在正尝试用 OAuth 2 替换 Basic Auth。我需要使用客户端凭证流。
以下是我遵循的步骤:
已向应用程序提供管理员同意。我在浏览器中启动了以下 URL,并使用管理员帐户提供了同意。
https://login.microsoftonline.com/common/oauth2/authorize? response_type=code+id_token& scope=openid& client_id=[Client ID]& redirect_uri=http://localhost/myapp/permissions& resource=https://outlook.office.com& prompt=admin_consent& response_mode=form_post& nonce=1234
同意后,我从响应中检索了
id_token
,并使用 JWT.io 对其进行了解码。从有效负载中,我记录了tid
.接下来我通过发送以下请求检索了访问令牌:
POST https://login.microsoftonline.com/[TID]/oauth2/token HTTP/1.1 cache-control: no-cache Content-Type: application/x-www-form-urlencoded Accept: */* Host: login.microsoftonline.com accept-encoding: gzip, deflate Connection: close client_id=[CLIENT ID]& client_secret=[CLIENT SECRET]& grant_type=client_credentials& resource=https%3A%2F%2Foutlook.office.com
使用访问令牌,我发出了与使用 Basic Auth 相同的请求,只是我将 Basic Auth header 替换为
Authorization: Bearer [Access Token]
我收到以下错误 (403 Forbidden
):
The token contains not enough scope to make this call.
我需要做什么来修复这个错误?
编辑 1: 我添加了 Use Exchange Web Services with full access to all mailboxes
应用程序权限,现在发送 SOAP 消息会导致 500 Internal Server Error
...
解决方案涉及以下内容:
添加
Use Exchange Web Services with full access to all mailboxes
应用程序权限,因为 EWS 不允许使用更精细的权限。正在为目标邮箱添加一个ExchangeImpersonation SOAP header。
正在设置 X-AnchorMailbox HTTP header。