I try to access shared Google Drive files through Python.
I have created an OAuth 2.0 ClientID as well as the OAuth consent.
I have copy-pasted this code: https://github.com/googleworkspace/python-samples/blob/master/drive/quickstart/quickstart.py
The authorization is successful, however, the Python code returns a blank list indicating there are no files in Google Drive, although there are many.
Should there be a difference because I am trying to access a shared folder, if yes, could it cause the error, and how this can be solved?
If not, is this the right approach? I read about API keys and Service Accounts as well, would it make sense to use either of them? Later this service I create will be used by other users on Databricks (running on AWS), and I do not know which solution would be the best one.
Thank you for your help!
Have you tried to use the PyDrive library?
https://pypi.org/project/PyDrive/
You can use the PyDrive wrapper library to get high level functions that you can use to access the Google Drive API.
PyDrive also uses OAuth2.0 and you can get setup with just a couple of lines:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
You can get a file like so:
# or download Google Docs files in an export format provided.
# downloading a docs document as an html file:
docsfile.GetContentFile('test.html', mimetype='text/html')
The Wrapper also allows you to create and upload files easily:
file1 = drive.CreateFile({'title': 'Hello.txt'})
file1.SetContentString('Hello')
file1.Upload() # Files.insert()
You can get more documentation and examples using the link I sent previously. Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With