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

余生颓废 提交于 2019-12-20 02:28:17

问题


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?


回答1:


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"]



回答2:


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)


来源:https://stackoverflow.com/questions/40198449/how-can-i-get-the-cloud-ml-service-account-programmatically-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!