【问题标题】:How to impersonate an admin user when using getClient() in the Google API NodeJS client在 Google API NodeJS 客户端中使用 getClient() 时如何模拟管理员用户
【发布时间】:2018-05-14 17:27:27
【问题描述】:

根据 defaultauth 示例中的 recommendation,我正在尝试访问已为其创建服务帐户的域的目录 api。这是我尝试连接的代码:

import { google } from 'googleapis'

const authClient = await google.auth.getClient({
    scopes: ['https://www.googleapis.com/auth/admin.directory.user.readonly']
})

const service = google.admin('directory_v1')

console.log(
    await service.users.list({
        auth: authClient,
        domain: <redacted>
    })
)

但是,当我尝试连接时,我收到一条错误消息,提示 Error: Not Authorized to access this resource/api。如果我删除了~/.google 中的creds.json 文件,错误将变为找不到凭据文件。此外,我可以使用相同的文件访问存储桶,所以我很确定我的本地环境设置正确,身份验证明智。过去几天,我还与支持团队 G Suite API 团队的某个人一起工作,他们向我保证我的域上的设置正确。

在网上环顾四周后,似乎我缺少的是在尝试连接我的服务帐户时冒充管理员帐户。我在网上找到了一些使用 JWT 身份验证策略执行此操作的示例,但我想继续使用默认身份验证客户端,以便抽象出实现细节。这可能吗?如果是这样,我需要改变什么?我尝试在两个调用(getClientlist)中设置subjectdelegationEmail

任何帮助将不胜感激。

【问题讨论】:

    标签: google-api-nodejs-client


    【解决方案1】:

    只需设置客户端对象的主题:

    authClient.subject = 'your email address'
    

    Google 的 api 文档因语言而异。没有标准。在 PHP 客户端中记录的内容可能在 nodejs 客户端中丢失,并且可能需要数小时才能找到如何做到这一点。

    【讨论】:

    • 我搜索了几个小时才找到这个。回想起来很明显,但谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多