状态码:禁止;原因:此用户或应用程序未获得请求授权

Status Code: Forbidden; Reason: The request is not authorized for this user or application

我正在尝试为特定用户订阅 Microsoft Teams Presence API。

请求Url:

https://graph.microsoft.com/beta/subscriptions

请求正文

{ 
  'changeType': 'created,updated', 
  'notificationUrl': 'https://<domain-name>/presence-notify/', 
  'resource': '/communications/presences/{id}',
  'expirationDateTime': '2020-09-10T07:37:13Z', 
  'clientState': 'secretClientState' 
}

回应

{
  "error": {
    "code": "ExtensionError",
    "message": "Operation: Create; Exception: [Status Code: Forbidden; Reason: The request is not authorized for this user or application.]",
    "innerError": {
      "date": "2020-09-09T11:27:27",
      "request-id": "c563f94d-3c10-4c09-be35-0d1993d9a112"
    }
  }
}

管理员请求并授予以下委派权限:

客户端证书图片

您需要指定encryptionCertificate。来自 documentation:

presence subscriptions require encryption. Subscription creation will fail if encryptionCertificate is not specified.

终于解决了这个问题。如错误所述 The request is not authorized for this user or application。所以我认为 Access Token 可能存在问题。实际上我是代表 App 调用 API,即 grant_type 作为 client_credentials 的令牌。相反,我应该代表用户调用 API,即使用 grant_type 作为 password 的令牌,正如 documentation 中明确提到的那样 权限被委派。我没有使用任何 encryptionCertificate 仍然订阅成功。