Google API: Why can't it find my file?

2019-08-20 05:59发布

So I am trying to use the Google Drive API, but I keep running into an error (Cannot access credentials.json: No such file or directory). I looked up what someone else did and they solved it by using an absolute file path. I tried this by making:

if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)    

Into:

if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets(r'C:\Users\FIEND\Documents\Google Sheets Script\client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)

Both of these got this error:

C:\Users\FIEND\AnacondaTests\lib\site-packages\oauth2client\_helpers.py:255: 
UserWarning: Cannot access credentials.json: No such file or directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
usage: ipykernel_launcher.py [--auth_host_name AUTH_HOST_NAME]
                         [--noauth_local_webserver]
                         [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT 
...]]]
                         [--logging_level 
{DEBUG,INFO,WARNING,ERROR,CRITICAL}]
ipykernel_launcher.py: error: unrecognized arguments: -f 
C:\Users\FIEND\AppData\Roaming\jupyter\runtime\kernel-aa1a9ef5-446a-46d0- 
b6fb-032bd6d673e7.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2


C:\Users\FIEND\AnacondaTests\lib\site- 
packages\IPython\core\interactiveshell.py:2918: UserWarning: To exit: use 
'exit', 'quit', or Ctrl-D.
 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

Here is the rest of the code for reference:

"""
Shows basic usage of the Sheets API. Prints values from a Google 
Spreadsheet.
"""
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# Setup the Sheets API
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
# Call the Sheets API
SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
RANGE_NAME = 'Class Data!A2:E'
result = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID,
                                         range=RANGE_NAME).execute()
values = result.get('values', [])
if not values:
    print('No data found.')
else:
    print('Name, Major:')
    for row in values:
        # Print columns A and E, which correspond to indices 0 and 4.
        print('%s, %s' % (row[0], row[4]))

Edit

Making an empty credentials.json file cleared most of the errors, but I am still getting an "error: unrecognized arguments".

Full error text:

usage: ipykernel_launcher.py [--auth_host_name 
AUTH_HOST_NAME]
                         [--noauth_local_webserver]
                         [--auth_host_port [AUTH_HOST_PORT 
[AUTH_HOST_PORT ...]]]
                         [--logging_level 
{DEBUG,INFO,WARNING,ERROR,CRITICAL}]
ipykernel_launcher.py: error: unrecognized arguments: -f 
C:\Users\FIEND\AppData\Roaming\jupyter\runtime\kernel- 
c568600c-15a8-4f33-a635-409e218e40bf.json

1条回答
三岁会撩人
2楼-- · 2019-08-20 07:03

You have to create an empty configuration.json file. Github Google Issue.

Please follow the following setup steps to get the authentication properly.

Step 1: Turn on the Drive API

  1. Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials.

  2. On the Add credentials to your project page, click the Cancel button.

  3. At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.

  4. Select the Credentials tab, click the Create credentials button and select OAuth client ID.

  5. Select the application type Other, enter the name "Drive API Quickstart", and click the Create button.

  6. Click OK to dismiss the resulting dialog.

  7. Click the file_download (Download JSON) button to the right of the client ID.

  8. Move this file to your working directory and rename it client_secret.json.

Documentation: https://developers.google.com/drive/v3/web/quickstart/python

UPDATE

Apparently, there is a conflict between argparse and Jupyter [Reference]

and oauth2client is using argparse Github Code Link

I have not used Jupyter myself, but if you can, try using your google drive api code independently without Jupyter.

查看更多
登录 后发表回答