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

前端 未结 2 1339
挽巷
挽巷 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"]
    

提交回复
热议问题