0

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: エラー: ユーザーは許可されていません。

なぜこのエラーが発生するのか、誰か助けてもらえますか? どうすればこれを修正できますか?

PS

  • Google Resource Manager API が有効になっています。
  • サービス アカウントには role=owner 権限があります。
4

1 に答える 1