I'm updating a script to call OAuth2-protected Google Cloud endpoints. The previous version assumed a single user previously authenticated by gcloud auth login and thus was able to use the default:
credentials = GoogleCredentials.get_application_default()
http = credentials.authorize(http)
However now I must do some calls as user A and some as user B. I can perform these steps in the shell to generate access tokens, but I would prefer to do it in the program directly:
gcloud auth login [email protected]
gcloud auth print-access-token [email protected]
Is there a way to generate two Credentials values for two different emails without having to run any shell commands?
You probably want to use
instead of
gcloud auth login.But if you are using gcloud credentials (not application default) then note that
is an interactive command. You choose user to login as in the browser not on the command line.
You can pre-login before hand and then use credentials you desire. For example:
this adds credentials to the credential store (Note that here
--accountis used just for validation making sure webflow chosen account is same as requested here). You can see all available credentials by runningThen you can use specific account on demand
Note that print-access-token is undocumented command and you should only use it for debugging.
Somewhat more advanced feature is to use configurations
You can create new ones by
then you can do
the added advantage that not only you can configure account but other attributes like project, compute zone, etc ...