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
You have to create an empty configuration.json file. Github Google Issue.
Please follow the following setup steps to get the authentication properly.
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.