6

In a Google Colab notebook, I am running a block of code which will take several hours to complete, and at the end a file will be uploaded to my Google drive.

The issue is that sometimes my credentials will expire before the code can upload the file. I have looked around and may have found some code that can perhaps refresh my credentials but I am not 100% familiar with the how Pydrive works and what exactly this code is doing.

Here is the code I am using so far to set my notebook up to access my Google Drive.

!pip install -U -q PyDrive

from google.colab import files
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

And this the code I use to upload the file

uploadModel = drive.CreateFile()
uploadModel.SetContentFile('filename.file')
uploadModel.Upload()

This is the code I found which may solve my issue (found here PyDrive guath.Refresh() and Refresh Token Issues )

if gauth.credentials is None:
    # Authenticate if they're not there
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    # Refresh them if expired
    print "Google Drive Token Expired, Refreshing"
    gauth.Refresh()
else:
    # Initialize the saved creds
    gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("GoogleDriveCredentials.txt")

So I am guessing the gauth.Refresh() line prevents my credentials from expiring?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
SantoshGupta7
  • 5,607
  • 14
  • 58
  • 116

1 Answers1

3

When a user authenticates your application. You are given an access token and a refresh token.

Access tokens are used to access the Google APIs. If you need to access private data owned by a user for example their google drive account you need their permission to access it. The trick with access tokens is they are short lived they work for an hour. Once the access token has expired it will no longer work and this is where the refresh token comes into play.

Refresh tokens for the most part do not expire as long as the user does not remove your consent to your application accessing their data though their google account you can use the refresh token to request a new access token.

This like elif gauth.access_token_expired: checks to see if the access token is expired or probably about to expire. If it is then gauth.Refresh() will refresh it. Just make sure you have get_refresh_token: True so that you have a refresh token

I am a little surprised that the library isn't doing this for you automatically. But I am not familiar with Pydrive. The Google APIs Python client library automatically refreshes the access token for you.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 1
    " Just make sure you have get_refresh_token: True so that you have a refresh token" I just looked this up. In the 'yaml' file ? Or is there in a way to do it directly in python. 'I am a little surprised that the library isn't doing this for you automatically. But I am not familiar with Pydrive' I think this may have been happening because sometimes the credentials don't expire for over an hour, sometimes up to 4 hours. It may be an instance where the code goes over an hour before uploading anything. – SantoshGupta7 Jul 19 '18 at 19:06
  • 1
    Any update on this ? a piece of code that works to get the refresh right? – agenis Jan 16 '21 at 14:55
  • I'm looking for a solution to that too – Leonardo Rick Mar 18 '21 at 05:15