Search code examples
authenticationautomationgoogle-oauthremote-servergcloud

Google Cloud SDK - Automate gcloud auth login web flow


After I install google cloud sdk, when I run 'gcloud auth login', it provides me a link to copy paste into my browser to obtain new credentials and authenticate my account.

However, I want to setup and use gcloud on a remote machine where a user will not be available to copy paste the link generated in the browser and paste the resultant verification code back into the terminal. How do I automate this without user intervention?

My account is an end user account i.e. not a service account.

Is there any other alternative to do this whereby I can pre-authenticate my account using the generated link to use in the browser on my local machine and then use the same configuration on the remote machine?

Thanks in advance!

Update based on Vilas's comments:

I am pretty sure I am using the right json key file because I am not modifying the name/contents after downloading it from the google developers console. I tried removing account@gmail.com from the above command. But still getting the same error

ERROR: (gcloud.auth.activate-service-account) The .json key file is not in a valid format. Your current active account [account@gmail.com] does not have any valid credentials.Please run:

  $ gcloud auth login

to obtain new credentials, or if you have already logged in with a
different account:

  $ gcloud config set account ACCOUNT

to select an already authenticated account to use.

Your current active account [account@company.com] does not have any valid credentials
Please run:

  $ gcloud auth login

to obtain new credentials, or if you have already logged in with a
different account:

  $ gcloud config set account ACCOUNT

to select an already authenticated account to use.

Solution

  • Here's a quick check to see if you have the right --key-file .json file: it must contain a client_email field/attribute.