I have a script that leverages the gcloud
tool to create service accounts for a project.
I'd like to be able to create GSuite groups and add the service accounts to them within the same script as creating the service accounts, so I don't have to do it manually in the web UI.
Preferably, I would like as much of this to be done via the CLI as possible, including the initial setup of service accounts and GSuite authentication etc.
There is a lot of conflicting information when it comes to authenticating between gcloud and gsuite and how to automate GSuite tasks using gcloud service accounts.
What is the process to:
- Get the required authentication information to submit requests to the GSuite API
- Give permissions/scopes to whatever is required to the obtain the above
- Actually use that auth to create a group/add a user to a group/etc.
I would prefer to do any API requests either using a CLI tool (such as gcloud
) or CURL. If a client SDK is required, I would prefer the NodeJS one.
Update
To clarify what I've tested so far, after wading through tons of out of date documentation on Google's developer portal, I:
- Enabled the "Admin SDK API" for my project in GCloud (Google regularly references the 'Directory' API, which isn't something that can be enabled in the GCloud APIs, so I assume they're referring to the Directory API within the Admin SDK API)
- Created a service account and gave it
owner
role for the project - Enable domain-wide delegation on the service account to get its Client ID
- In the Google Admin (GSuite) console, I gave the service account client ID the scope
https://www.googleapis.com/auth/admin.directory.group
in both "Security -> Manage API client access" and "Security -> API Controls -> Domain-wide Delegation" - Used the oauth2l tool with curl to generate a bearer token with the scope
admin.directory.group
- Attempted to use that bearer token to create a group using the following command:
curl --request POST 'https://www.googleapis.com/admin/directory/v1/groups' --header "$(oauth2l header --json /home/<file containing the secret for the service account> --scope admin.directory.group)" --header 'Accept: application/json' --header 'Content-Type: application/json' --data '{"email":"test@my-gsuite-domain.com"}' --compressed
- Gsuite returned a nice handy 403
Not Authorized to access this resource/api
.
Over all of the Google documentation they advise you to try these things out using the API Explorer, which doesn't exist anymore.
In the directory API pre-reqs page it describes setting up the API for the Admin console - The options described there also do not exist.