小编Ale*_*zis的帖子

如何在 Google API NodeJS 客户端中使用 getClient() 时模拟管理员用户

根据defaultauth 示例中的建议,我尝试访问我已为其创建服务帐户的域的目录 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>
    })
)
Run Code Online (Sandbox Code Playgroud)

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

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

任何帮助将不胜感激。

google-api-nodejs-client

6
推荐指数
1
解决办法
1563
查看次数

标签 统计

google-api-nodejs-client ×1