I am trying to use PyDrive to get a list of all files in my Google Drive. I have read through the docs and completed all steps. I have client secrets.json saved down, but I continue to get the following error. The code I am using is:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
# Creates local webserver and auto handles authentication
drive = GoogleDrive(gauth)
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
print 'title: %s, id: %s' % (file1['title'], file1['id'])
The error I am getting is, how to do I fix this?
Traceback (most recent call last):
File "C:\Users\mydrive\Documents\Python\Google_Drive.py", line 5, in <module>
gauth.LocalWebserverAuth()
File "build\bdist.win-amd64\egg\pydrive\auth.py", line 67, in _decorated
self.GetFlow()
File "build\bdist.win-amd64\egg\pydrive\auth.py", line 345, in GetFlow
self.LoadClientConfig()
File "build\bdist.win-amd64\egg\pydrive\auth.py", line 294, in LoadClientConfig
self.LoadClientConfigFile()
File "build\bdist.win-amd64\egg\pydrive\auth.py", line 314, in LoadClientConfigFile
raise InvalidConfigError('Invalid client secrets file %s' % error)
InvalidConfigError: Invalid client secrets file File not found: "client_secrets.json"
Based on the error log, your program cannot find the file: 'client_secrets.json'. This file is essential as it helps identify your program to the Google API.
Steps to get the authentication going:
Request Google Drive API access through Google Cloud Console
Steps explained at: https://pythonhosted.org/PyDrive/quickstart.html
I am copying and updating the instructions from the original page in case the site is made unavailable in the future:
Instructions for getting Google Drive API access
Go to Google Developers Console - https://console.developers.google.com and create a new project
Click on Enable and manage APIs, click on Drive API, then click on Enable API.
In API Manager, click on Credentials on the left panel. Select Add Credentials, choose OAuth 2.0 client ID, then Web Application. You may need to configure a consent screen, where the required part is the Product name, and the rest you can leave blank.
In the Create client ID window, with Web application selected as Application type, specify the Name for your application, put http://localhost:8080
for Javascript origins and http://localhost:8080/
for redirect URIs. IMPORTANT: One of these ends with /, the other does not.
Download the client_secrets.json file from Google Developers Console
Go to Google Developers Console -https://console.developers.google.com and find the Use Google API section and click on Enable and manage APIs. Select Credentials on the left panel. You should see a list of your OAuth 2.0 client IDs. Check off the one you've created in step 1, and click on the download JSON button(looks like an arrow down icon). Rename the downloaded file to client_secrets.json.
Place the client_secrets.json into the project directory
It is best to place the downloaded client_secrets.json file in the same directory as your python program that has the following line: gauth.LocalWebserverAuth()
Once you got the authentication going, I would recommend you use the code from the answer https://stackoverflow.com/a/24542604/820173 to save the credentials so that you don't have to authenticate every time you run your code.
For more advanced users, it is possible to create a settings.yaml file with advanced credential savings techniques. Examples described in the test files for the PyDrive project: https://github.com/googledrive/PyDrive/tree/master/pydrive/test I would like to mention that this advanced stuff is not necessary to get things going, all you need are the 3 steps explained in this answer.