Reputation: 149
I'm working on a Python (3.6) project using Google Colaboratory notebook and I need to access files on Google Drive. Using the code below I can read files on Drive but it asks for a security code each time the code block is run, so whenever the session times-out I need to get the security code again.
drive.mount('/content/drive')
Is there a better and more convenient way to do this? Constant authentication is pretty annoying.
Upvotes: 4
Views: 1697
Reputation: 765
You can try using PyDrive. PyDrive is a wrapper library of google-api-python-client that simplifies many common Google Drive API tasks.
The snippet below is a snippet from this example. It demonstrates the authentication part of the code.
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
If you need more examples, you can visit the PyDrive documentation.
Upvotes: 3