Search code examples
pythongoogle-apigoogle-oauthgoogle-directory-api

How can I access Google Directory API using service account json


I have a service account key json file like following:

{
  "type": "service_account",
  "project_id": "project-id",
  "private_key_id": "key-id",
  "private_key": "-----BEGIN PRIVATE KEY-----\nprivate-key\n-----END PRIVATE KEY-----\n",
  "client_email": "service-account-email",
  "client_id": "client-id",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/service-account-email"
}

How can I access Google Directory API: https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list

using service account key json file using python.


Solution

  • To access the Directory API using a service account, you have to use domain-wide delegation. See Perform Google Workspace Domain-Wide Delegation of Authority for details.