Error: User is not authorized while creating GCP project using service account
Error: User is not authorized while creating GCP project using service account
我正在尝试使用 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: Error: User is not authorized.
谁能帮我解决这个错误?我该如何解决这个问题?
P.S.
- Google 资源管理器 API 已启用。
- 服务帐户具有 role=owner 权限。
您正在组织内创建项目。用户在组织中是否具有项目创建者角色?
创建组织后,您域中的所有用户都会自动获得组织级别的 Project Creator 和 Billing Account Creator IAM 角色。这使您域中的用户可以继续创建项目而不会中断。
组织管理员将决定何时开始积极使用组织。然后他们可以更改默认权限并根据需要实施更严格的政策
此外,如果您使用 service account (SA) 进行身份验证,则 SA 需要具有角色
我正在尝试使用 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: Error: User is not authorized.
谁能帮我解决这个错误?我该如何解决这个问题?
P.S.
- Google 资源管理器 API 已启用。
- 服务帐户具有 role=owner 权限。
您正在组织内创建项目。用户在组织中是否具有项目创建者角色?
创建组织后,您域中的所有用户都会自动获得组织级别的 Project Creator 和 Billing Account Creator IAM 角色。这使您域中的用户可以继续创建项目而不会中断。
组织管理员将决定何时开始积极使用组织。然后他们可以更改默认权限并根据需要实施更严格的政策
此外,如果您使用 service account (SA) 进行身份验证,则 SA 需要具有角色