【问题标题】:Google API: Why can't it find my file?Google API:为什么找不到我的文件?
【发布时间】: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


    【解决方案1】:

    您必须创建一个空的 configuration.json 文件。 Github Google Issue.

    请按照以下设置步骤正确获取身份验证。

    第 1 步:开启 Drive API

    1. 使用此向导在 Google Developers Console 中创建或选择一个项目并自动启用 API。单击继续,然后转到凭据。

    2. 在将凭据添加到您的项目页面上,单击取消按钮。

    3. 在页面顶部,选择 OAuth 同意屏幕选项卡。选择一个电子邮件地址,如果尚未设置,请输入产品名称,然后单击“保存”按钮。

    4. 选择凭据选项卡,单击创建凭据按钮并选择 OAuth 客户端 ID。

    5. 选择应用类型Other,输入名称“Drive API Quickstart”,点击创建按钮。

    6. 单击“确定”关闭生成的对话框。

    7. 单击客户端 ID 右侧的 file_download(下载 JSON)按钮。

    8. 将此文件移动到您的工作目录并将其重命名为 client_secret.json。

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

    更新

    显然argparse和Jupyter有冲突[Reference]

    并且 oauth2client 正在使用 argparse Github Code Link

    我自己没有使用过 Jupyter,但如果可以,请尝试在没有 Jupyter 的情况下独立使用您的 google drive api 代码。

    【讨论】:

    • 嘿,谢谢@x899,创建空的 configuration.jason 文件,帮助清除了大部分错误。不幸的是,我仍然收到“错误:无法识别的参数”。
    【解决方案2】:

    只需复制您的凭据文件并粘贴到 c 驱动器的同一文件夹中即可。

    【讨论】:

      猜你喜欢
      • 2015-12-02
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2018-02-14
      • 2018-02-17
      • 2013-07-11
      • 2017-10-23
      • 1970-01-01
      相关资源
      最近更新 更多