1

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I using this docs: cloud.google.com/resource-manager/reference/rest/v1/projects/… I have done it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.