跨GCP项目服务账户使用Google Wallet Node.js客户端报错求助
跨项目ServiceAccount调用Google Wallet Node.js API的配额项目配置问题
问题描述
使用来自A项目的ServiceAccount调用B项目的Google Wallet API,已为该ServiceAccount授予B项目的对应操作角色。Python中通过Credentials.with_quota_project("B-project")指定目标项目后调用成功,但Node.js中在GoogleAuth实例里设置projectId后,仍报错提示ServiceAccount所在的A项目未启用Wallet API,与Python未设置配额项目时的报错一致。
解决方案
Node.js中GoogleAuth的projectId参数并非对应Python的with_quota_project,需要显式指定quotaProjectId来设置配额项目(即目标项目B):
修改后的代码示例
const projectId = 'target-project' // 你的目标项目ID const scopes = ['https://www.googleapis.com/auth/wallet_object.issuer'] const credentials = JSON.parse(googleWalletCredentials) const auth = new GoogleAuth({ credentials, scopes, quotaProjectId: projectId // 关键:使用quotaProjectId指定目标项目 }) this.client = google.walletobjects({ version: 'v1', auth: auth })
备选方案(手动设置Auth客户端的配额项目)
如果上述配置仍不生效,可以手动获取Auth客户端并设置quotaProjectId:
const auth = new GoogleAuth({ credentials, scopes }) // 手动获取客户端并设置配额项目 const authClient = await auth.getClient() authClient.quotaProjectId = projectId this.client = google.walletobjects({ version: 'v1', auth: authClient })
验证要点
- 确认目标项目(target-project)已启用Google Wallet API
- 确认ServiceAccount在目标项目中拥有正确的角色权限(如
Wallet Object Admin)
内容的提问来源于stack exchange,提问作者Paulw11
相关产品推荐
相关产品推荐

