【问题标题】:Does the Gmail API support JWT?Gmail API 是否支持 JWT?
【发布时间】:2014-10-22 20:12:19
【问题描述】:

我想使用 NodeJS 访问 Gmail API。

我正在使用服务器到服务器的方法(请参阅this),但是当我执行下面的代码时,我得到了一个 backEndError,来自 Google API 的代码 500。

有什么想法吗?

var authClient = new google.auth.JWT(
    'email',
    'key.pem',
    // Contents of private_key.pem if you want to load the pem file yourself
    // (do not use the path parameter above if using this param)
    'key',
    // Scopes can be specified either as an array or as a single, space-delimited string
    ['https://www.googleapis.com/auth/gmail.readonly']
);

authClient.authorize(function(err, tokens) {
    if (err) 
        console.log(err);

    gmail.users.messages.list({ userId: 'me', auth: authClient }, function(err, resp) {
    // handle err and response
    if (err) {
        console.log(err);
});

【问题讨论】:

  • +1 表示好问题。我不知道,我会第二次通过developers.google.com/accounts/docs/OAuth2Login 并查看所有步骤是否符合预期(这部分在您的代码中不可见)。这可能会失败的一种情况是“我”帐户使用两步身份验证。在这种情况下,软件代理需要使用“我”(developers.google.com/google-apps/help/faq/auth#what_twostep)仅为它们创建的特殊令牌
  • 我也在尝试使用 JWT 但得到access_denied Requested client not authorized.
  • 请问您提供给new google.auth.JWT 的参数?我使用key.client_email 作为我的第一个参数,null 作为第二个参数,key.private_key 作为第三个参数,然后是作用域。我在哪里告诉它我应该从哪个用户那里获取邮件?我需要从域内的各个帐户中获取电子邮件。

标签: node.js oauth-2.0 google-oauth jwt gmail-api


【解决方案1】:

是的,我也有同样的问题。如果我使用范围“https://mail.google.com”,我会得到 ​​p>

403
{
  "error" : "access_denied",
  "error_description" : "Requested client not authorized."
}

如果我使用范围“https://mail.google.com/”(注意末尾的 /),我会得到

403
 'Forbidden'

好像和使用JWT和服务帐号有关。

【讨论】:

    猜你喜欢
    • 2019-05-11
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 2023-04-11
    • 2019-08-06
    • 2017-02-05
    相关资源
    最近更新 更多