微软图形客户端凭据 - 代表用户发送电子邮件时出现 oauth 错误

Raf*_*fiM 2 javascript microsoft-graph-api

我成功地为 Microsoft Graph 设置了 OAuth 2.0 客户端凭据,我获得了身份验证令牌没问题,订阅了多个用户的电子邮件 webhook,并收到了这些电子邮件通知。

在 Azure 门户中,我将应用程序设置为具有“以任何用户身份发送邮件”的 Microsoft Graph 应用级别权限。

我正在尝试使用 Graph 转发电子邮件,但出现BadRequest错误:

{
  "error": {
    "code": "BadRequest",
    "message":
      "Current authenticated context is not valid for this request. This occurs when a request is made to an endpoint that requires user sign-in. For example, /me requires a signed-in user.  Acquire a token on behalf of a user to make requests to these endpoints.  Use the OAuth 2.0 authorization code flow for mobile and native apps and the OAuth 2.0 implicit flow for single-page web apps.",
    "innerError": {
      "request-id": "a64c4533-9389-4284-8cf5-68fcadf21832",
      "date": "2018-05-27T17:34:29"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我发送请求的方式:

postData(
    `${graphVersion}/me/messages/${req.body.emailUid}/forward`,
    global.accessToken,
    JSON.stringify(forwardConfiguration),
    (requestError, data) => {
        if (requestError) {
            logger.log('error', "Failed to forward email: " + JSON.stringify(requestError));
        } else {
            logger.log("info", "Successfully forwarded email from " + req.body.email);
        }
    }
);
Run Code Online (Sandbox Code Playgroud)

Mar*_*eur 5

您不能/me与客户端凭据一起使用。

这是因为/me只是 的别名/user/{currentUser}。当您使用客户端凭据时,API 也没有“用户”来映射此别名。

您需要明确指定user您想要的:

/users/{userId or userPrincipalName}/messages
Run Code Online (Sandbox Code Playgroud)