4

When using Google Colaboratory with Google Cloud, one has to authenticate itself:

from google.colab import auth
auth.authenticate_user()
print('Authenticated')

The result of running that cell is:

Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/......

Enter verification code: 

Currently I have to grant it access and enter a new password every time I start a new notebook which makes me think I'm doing something wrong.

Is there a way to permanently allow the Google Cloud SDK access to my Google Account?

Bob Smith
  • 36,107
  • 11
  • 98
  • 91
pelicanactor
  • 115
  • 1
  • 9

1 Answers1

3

You can save credentials in the form of an API key that you write a file on disk. This still requires running a cell in each notebook to authenticate, but at least you don't have to click through the authentication flow each time.

See Getting Started with Authentication for creating an API key for a service account.

Then, in your notebook, write something like the following:

import os
storage_auth_info = r"""YOUR API KEY HERE"""
with open('/tmp/storage_auth_info.json', 'w') as f:
  f.write(storage_auth_info)
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/tmp/storage_auth_info.json'

This will let anyone who has access to your notebook use the saved credentials, so use this with caution, e.g., consider only giving the service account read-only access to your data.

shoyer
  • 9,165
  • 1
  • 37
  • 55