Reputation: 21
I want to create a Google Cloud project using Google Cloud API Python client.
Steps:
gcloud beta auth application-default login
command to get application default credentials by login onto the browser.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.
Upvotes: 1
Views: 788
Reputation: 140
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:
resourcemanager.projects.create
permission.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.
Upvotes: 3