问题
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