简体   繁体   中英

How can take the first step create a project on google cloud using Python API client

I want to create a Google Cloud project using Google Cloud API Python client.

Steps:

  • Run gcloud beta auth application-default login command to get application default credentials by login onto the browser.
  • Enable Resource Manager API on my GCP console.

Here's the working Python code:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
...

credentials = GoogleCredentials.get_application_default()
service = discovery.build('cloudresourcemanager', 'v1', credentials=credentials)
        project_body = {
            'name': 'Api Project',
            'projectId': 'api-9837'
        }
        request = service.projects().create(body=project_body)
        request.execute()
        pprint(request)

But I can't take the first step.

In order to make it easier to help you, please share always the errors/messages you receive.

In the docs: Creating and managing projects - Creating a project :

  1. You must have the resourcemanager.projects.create permission.
  2. You can create a new project using the Cloud Console, the gcloud command-line tool, or the projects.create() method.
...
from googleapiclient import discovery
from oauth2client.client import OAuth2Credentials as creds
crm = discovery.build(
    'cloudresourcemanager', 'v1', http=creds.authorize(httplib2.Http()))

operation = crm.projects().create(
body={
    'project_id': flags.projectId,
    'name': 'my project'
}).execute()

...

For Service Accounts:

You can use a service account to automate project creation. Like user accounts, service accounts can be granted permission to create projects within an organization. Service accounts are not allowed to create projects outside of an organization and must specify the parent resource when creating a project. Service accounts can create a new project using the gcloud tool or the projects.create() method.

There are slightly differences in your code vs the mentioned in the Docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM