19

I'm using Google Colab and need to restart my notebook at least once a day due to their usage limits.

To mount my Google Drive I have the following code:

from google.colab import drive
drive.mount('drive')

I then get a prompt:

Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=xxxxxxxxx....

Enter your authorization code: ___________________________________________________


How can I authorise only once and have that authorisation remembered?

Ideally, that authorisation will have already happened as I'm signed in to Gmail and I can just specify the account email address of the Drive to mount.

However any solution of persistent authorisation where I don't store the auth code in the notebook would be great.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242

1 Answers1

10

You can't set it to only authenticate once and stay that way for a new runtime, because Colab runs on a VM that is recycled periodically. You can make sure force_remount is set to False so it doesn't unnecessarily ask you to reauthorize:

drive.mount('/content/gdrive', force_remount=False)

But any time you reset the runtime, you will need to reauthenticate with a different authorization code.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
ambitiousdonut
  • 384
  • 5
  • 10
  • 9
    Shouldn't there be a way to store the app key & auth token in code so it can be restored? My other google drive APIs don't need to be constantly reactivated... – Kevin Mar 06 '20 at 01:10
  • As far as I can see this option has now been enabled. When a drive is mounted it will remain mounted. I don't know if there is a time limit on this though. – JaredS May 30 '20 at 14:10
  • @JaredS Only for that session. If the session has expired, you'll have to re-authenticate – ambitiousdonut Sep 09 '20 at 23:58
  • Doesn't this solve the problem: https://stackoverflow.com/questions/52808143/colab-automatic-authentication-of-connection-to-google-drive-persistent-per-n – GoPackGo Mar 04 '21 at 23:10
  • Doesn't this solve the problem. – icalvete Apr 22 '22 at 06:24