Поэтому я пытаюсь использовать Google Drive API, но продолжаю сталкиваться с ошибкой (Невозможно получить доступ к credentials.json: нет такого файла или каталога). Я посмотрел, что сделал кто-то другой, и они решили это, используя абсолютный путь к файлу. Я попробовал это сделать:
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
В
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)
Оба они получили эту ошибку:
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)
Вот остаток кода для справки:
"""
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]))
Редактировать
Создание пустого файла credentials.json устраняет большинство ошибок, но я все еще получаю сообщение «ошибка: нераспознанные аргументы».
Полный текст ошибки:
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