12

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"
Trying_hard
  • 8,931
  • 29
  • 62
  • 85

8 Answers8

17

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:

  1. 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.

  2. 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.

  3. 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.

Community
  • 1
  • 1
IvanD
  • 7,971
  • 4
  • 35
  • 33
  • I put it in \Python27 and in the PyDrive directory, also in the same directory where I'm executing the code. But still getting InvalidConfigError: Invalid client secrets file File not found: "client_secrets.json" – jason May 27 '16 at 06:12
  • I didn't see this but this is exactly what I did, except I used tuxdrive. – jimh Feb 25 '20 at 20:09
  • Failed to find "code" in the query parameters of the redirect. When opening the auth url – Lalle Feb 26 '23 at 20:16
4

I too had all steps done as needed but was getting the same error. Then I added a debug print line inside PyDrive/clientsecrets.py -> _loadfile() to see the current working directory. Then noticed it was expecting the file relative to my Django app's URL pattern.

Eg: if this is how my Django URLs were defined for my app 'documents':

urlpatterns = 
[
..
    path('testGdrive/',         views.testGdrive),
..
]

eg URL: /localhost:8000/documents/testGdrive/

If was expecting the file at 'documents/testGdrive/', whereas my JSON file was directly under the 'documents' folder('testGdrive' was a virtual folder)

Therefore, setting the full path solved this:

# don't start path with '/', as this causes it to look relative to the root folder    
client_json_path = 'documents/client_secrets.json'    
GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = client_json_path
2

First go to: https://console.developers.google.com/project

then go your project -> Apis and authentication -> credentials. Here you can download your client_secrets.json.

Now copy this file (client_secrets.json) in the same directory that you are executing your .py

Mateo
  • 29
  • 2
2

I had the same problem. The reason why you cannot log in is here:

InvalidConfigError: Invalid client secrets file File not found: "client_secrets.json"

You need to change your credential file name from:
client_secret_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com.json

to:
client_secrets.json

Cheers, Daddy

GoDaddyGo
  • 167
  • 3
  • 11
0

Make sure that you call the file client_secret and not client_secret.json which may actaully be saved as client_secret.json.JSON when you save the file and you may go bananas for several hours trying to figure this out.

user3814004
  • 139
  • 1
  • 10
0

Just had this very same problem here. No matter where I'd put the client_secrets.json it wouldn't run (maybe something related to my PyCharm version, not sure). So I've done something highly not recommended but was what saved me. I got into PyDrive auth.py (in my computer it was located in this path: /Users/MyUserName/anaconda3/lib/python3.6/site-packages/pydrive/auth.py) and changed the code right where the problem was. Function LoadClientConfigFile looked like this:

if client_config_file is None:
      client_config_file = self.settings['client_config_file']
    try:
      client_type, client_info = clientsecrets.loadfile(client_config_file)
    except clientsecrets.InvalidClientSecretsError as error:
      raise InvalidConfigError('Invalid client secrets file %s' % error)

So I added a line to, in case it doesn't load, just open the file where it is located:

if client_config_file is None:
  client_config_file = self.settings['client_config_file']

try:
  client_type, client_info = clientsecrets.loadfile(client_config_file)
except:
  try:
    client_config_file="/Users/MyUserName/Downloads/client_secrets.json" # <--- here is where you should put your json file path
    client_type, client_info = clientsecrets.loadfile(client_config_file)
  except:
    raise InvalidConfigError('Invalid client secrets file %s' % error)
aabujamra
  • 4,494
  • 13
  • 51
  • 101
0

If you have already downloaded the credential file, renamed it to "client_secrets.json" and have kept it in the same folder as your script, and still getting this error then it might be helpful to try the below.

import os 
print (os.getcwd()) # to check the current working directory, if the current working directory above is different than your folder containing the script then change the working directory to your script directory.

os.chdir('/script_directory_path') # replace '/script_directory_path' with your original script path, and place this code above 'gauth = GoogleAuth()' in your script.
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

You can check if you named it correctly forexample for me it was client_secret.json instead of client_secrets.json