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

前端 未结 2 1316
挽巷
挽巷 2021-01-20 04:09

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?

相关标签:
2条回答
  • 2021-01-20 04:27

    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"]
    
    0 讨论(0)
  • 2021-01-20 04:41

    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)
    
    0 讨论(0)
提交回复
热议问题