【问题标题】:Error: User is not authorized while creating GCP project using service account错误:使用服务帐户创建 GCP 项目时用户未获得授权
【发布时间】:2020-02-18 14:12:50
【问题描述】:

我正在尝试使用 Google API 以编程方式创建 GCP 项目。下面是示例代码:

const {JWT} = require('google-auth-library')

async function main (keyFile = {PATH_TO_CREDENTIAL_FILE}) {
  const keys = require(keyFile)
  const client = new JWT({
    email: keys.client_email,
    key: keys.private_key,
    scopes: ['https://www.googleapis.com/auth/cloud-platform']
  })
  const url = 'https://cloudresourcemanager.googleapis.com/v1beta1/projects/'
  const data = {
    projectId: 'my-first-project',
    name: 'My First Project',
    parent: {
      type: 'organization',
      id: {ORGANIZATION_ID}
    }
  }
  const res = await client.request({
    url,
    method: 'POST',
    data: JSON.stringify(data)
  })
  console.log('project Info:')
  console.log(res.data)

  const tokenInfo = await client.getTokenInfo(client.credentials.access_token)
  console.log('tokenInfo', tokenInfo)
}

const args = process.argv.slice(2)
main(...args).catch(console.error)

运行此代码后,我收到以下错误:

UnhandledPromiseRejectionWarning:错误:用户未被授权。

谁能帮助我为什么会收到此错误?我该如何解决这个问题?

附言

  • Google 资源管理器 API 已启用。
  • 服务帐号拥有 role=owner 权限。

【问题讨论】:

  • 此用户是否属于某个组织,并且您正在组织中创建此项目?如果否,那么您必须在 Google Cloud Console 中创建项目。不属于组织的 IAM 成员无法使用 CLI 或 API 创建项目。
  • 是的,用户是组织的一部分,是的,我正在组织中创建项目。

标签: node.js google-cloud-platform google-auth-library google-auth-library-nodejs


【解决方案1】:

您正在组织内创建一个项目。用户在组织中是否具有项目创建者角色?

创建组织后,您域中的所有用户都会自动获得组织级别的 Project Creator 和 Billing Account Creator IAM 角色。这使您域中的用户可以继续创建项目而不会中断。

组织管理员将决定何时开始积极使用该组织。然后,他们可以根据需要更改默认权限并实施更严格的政策

此外,如果您使用 service account (SA) 进行身份验证,则 SA 需要具有角色

【讨论】:

  • 是的,用户拥有项目所有者权限。另外,我在创建 SA 的项目下给了 SA 角色=所有者。我是否也需要在组织下担任 SA 角色?
  • 授予资源管理>项目创建者权限解决了这个问题。谢谢!
猜你喜欢
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 2019-03-06
  • 2012-05-20
  • 1970-01-01
  • 2019-01-01
相关资源
最近更新 更多