javascript - Microsoft Graph 客户端凭据 - 获取代表用户发送电子邮件的 oauth 错误

标签 javascript microsoft-graph-api

我成功地为 Microsoft Graph 设置了 OAuth 2.0 客户端凭据,我获得了授权 token 没有问题,订阅了接收多个用户的电子邮件 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"
    }
  }
}

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

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

最佳答案

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

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

您需要明确指定您想要的用户:

/users/{userId or userPrincipalName}/messages

关于javascript - Microsoft Graph 客户端凭据 - 获取代表用户发送电子邮件的 oauth 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50554876/

相关文章:

microsoft-graph-api - 如何使用 Graph API 读取 Azure B2C 自定义属性(适用于 Azure AD Graph)

microsoft-graph-api - 在 Microsoft Teams 应用程序中访问通话记录

javascript - 来自服务器的 Phonegap 警报弹出窗口

javascript - History.js 和 url 编码

javascript - 用户事件后 Soundcloud 自定义播放器不在移动 safari 上播放

azure - 有没有办法削弱我在 Azure AD 上创建的应用程序的权限?

javascript - 组合函数的执行顺序

javascript - 是否可以在 python 中将包含 JavaScript 图表的 html 文件转换为 PDF?

outlook - Microsoft Graph 创建事件 - 说明中的换行符出现在 Outlook 日历中,但不在 Teams 日历中

c# - SDK v5 中的 ms.Graph 'groups' 中的分页