【发布时间】:2018-04-30 04:28:58
【问题描述】:
所以我正在尝试使用 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
【问题讨论】:
标签: python python-3.x google-sheets-api