Jeremy Lewi
Jeremy Lewi

Reputation: 6776

How can I get the Cloud ML service account programmatically in Python?

The Cloud ML instructions show how to obtain the service account using shell commands. How can I do this programmatically in Python? e.g. in Datalab?

Upvotes: 3

Views: 400

Answers (2)

Hafizur Rahman
Hafizur Rahman

Reputation: 2370

It is a very important step if you wan to automate the process through your python code.

The followings worked for me without 'v1beta1'. Don't forget to change your default or current project_id by using your real project id (e.g., 'customer-analytics-123')

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

credentials = GoogleCredentials.get_application_default()
my_project_id = 'my_current_project_id' # change according to your project id
projects = 'projects/' + my_project_id
ml_client = discovery.build(
    'ml',
    'v1',        
    requestBuilder=http.HttpRequest,
    credentials=credentials)
projs = ml_client.projects()
response = projs.getConfig(name = projects).execute()
SERVICE_ACCOUNT = response.get('serviceAccount')
print('Your Service Acc:', SERVICE_ACCOUNT)

Upvotes: 0

Jeremy Lewi
Jeremy Lewi

Reputation: 6776

You can use Google Cloud's Python client libraries to issue the getConfig request.

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

credentials = GoogleCredentials.get_application_default()

ml_client = discovery.build(
    'ml',
    'v1beta1',
    requestBuilder=http.HttpRequest,
    credentials=credentials)
p = ml_client.projects()
config = p.getConfig(name="projects/my-project").execute()
SERVICE_ACCOUNT = config["serviceAccount"]

Upvotes: 4

Related Questions