For a more detailed version of the steps below visit: https://developers.google.com/admin-sdk/directory/v1/guides/delegation
- Enable the necessary APIs (calendar, drive, gmail, etc.) in Cloud Console
- Create a service account, download the json key file, and enable domain wide delegation (DwD)
- Determine the required scopes (calendar readonly, drive read/write, etc.)
- In the G Suite console, an admin authorizes the service account (client ID) with the specified scopes
- The admin also identifies an account — with proper admin privileges — that the service account will impersonate (when you want to access user's data without manual authorization from the user)
With the above in place, you or another developer, who has the json key file, can run the python code.
For documentation on how to do with Java see: https://developers.google.com/api-client-library/java/google-api-java-client/oauth2#service_accounts
"unauthorized_client: Client is unauthorized to retrieve access tokens using this method."
Most likely means the service account hasn't been authorized in the G Suite console properly. Check that the scopes are the same between the app and G Suite.
"Not Authorized to access this resource/api"
The delegate user account doesn't have proper privileges to access the requested resource. https://stackoverflow.com/questions/42784640/client-is-unauthorized-to-retrieve-access-tokens-using-this-method-gmail-api-c-s
Gmail API returns 403 error code and “Delegation denied for...”
Thanks for this code. google-admin-directory.py
I have been struggling getting the quickstarts working with the admin sdk and a service account. Do I always need to have a delegate ? The docs "vaguely" suggest all I need is the secrets file and then get an access token using that.