microsoft graph客户端凭据 - 代表用户发送oauth错误发送电子邮件

时间:2018-05-27 17:45:38

标签: javascript microsoft-graph

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

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

我正在尝试使用图表转发电子邮件,但收到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"
    }
  }
}

这就是我发送请求的方式:

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);
        }
    }
);

1 个答案:

答案 0 :(得分:4)

您无法将/me与客户端凭据一起使用。

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

您需要明确指定所需的user

/users/{userId or userPrincipalName}/messages