【问题标题】:GSuite service account with DwD unauthorised for accessing user's Gmail account带有 DwD 的 GSuite 服务帐户未经授权访问用户的 Gmail 帐户
【发布时间】:2020-01-06 13:12:52
【问题描述】:

我有一个启用了域范围委派的 G Suite 服务帐号,并且我想模拟域中的用户。然而,我的每一次尝试都遇到了一个错误,说我未经授权。有没有人经历过这种情况并且可能知道发生了什么?

我遵循了these 的指示,也遵循了these。我创建了一个新的服务帐户,(如上所述)启用了 DwD,并在管理控制台中添加了必要的范围:https://mail.google.comhttps://www.googleapis.com/auth/gmail.settings.sharinghttps://www.googleapis.com/auth/gmail.settings.basichttps://www.googleapis.com/auth/admin.reports.audit.readonly

(此外,域已验证。)

从那里,我尝试使用以下代码在 NodeJS 客户端中授权此帐户:

const {google} = require('googleapis');
const fs = require('fs');
const auth = JSON.parse(fs.readFileSync('xxx.json'));

const jwt = new google.auth.JWT(
    auth.client_email, 
    null, 
    auth.private_key, 
    [
        'https://mail.google.com/',
        'https://www.googleapis.com/auth/gmail.settings.sharing',
        'https://www.googleapis.com/auth/gmail.settings.basic',
        'https://www.googleapis.com/auth/admin.reports.audit.readonly'
    ],
    'user@domain.com'
);

jwt.authorize((err, res) => {
    if (err) console.log(err);
    else console.log(res);
});

如果我删除user@domain.com 并尝试在不冒充电子邮件的情况下进行授权,它会起作用;我收到一个访问令牌。但是,出于我的目的,我需要能够模拟,如果我尝试这样做,我会收到带有以下消息的 401:

GaxiosError: unauthorized_client: Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.

据我所知,服务帐户应该被授权模拟域上的用户。有谁知道为什么会发生这种情况?

【问题讨论】:

  • 1) 展示一个完整的示例,您调用的 API 会产生影响。 2) 我建议使用 OAuth 访问令牌而不是签名的 JWT。 3)你真的需要范围'https://mail.google.com/吗? 4 从发送电子邮件和gmail.send 范围等更简单的事情开始 5) G Suite 帐户是否是超级管理员并且您已登录 G Suite,接受 TOS 等? 6) 编辑您的问题并通过这些问题进行改进。
  • 提示:按照您的第一个链接中的步骤,如果您弄错任何项目,模拟将不起作用。我会删除服务帐户并重新开始。我还将使用其中一个代码示例来验证您是否正确设置了 SA。
  • 嗨@JohnHanley,感谢您的回复。事实证明,我的问题是一个相当愚蠢的问题:当我在 Google 管理控制台中输入作用域时,我用空格分隔它们,而它们应该用逗号分隔,例如,'@987654327 @, https://...'

标签: node.js rest google-cloud-platform google-workspace google-api-nodejs-client


【解决方案1】:

最后,是我愚蠢。在管理控制台中,我一直用空格分隔作用域,而实际上它们应该用逗号分隔:'https://mail.google.com, https://...'

【讨论】:

    猜你喜欢
    • 2020-11-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    相关资源
    最近更新 更多