Google API:为什么找不到我的文件? [英] Google API: Why can't it find my file?

查看:149
本文介绍了Google API:为什么找不到我的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试使用Google Drive API,但始终遇到错误(无法访问凭据.json:没有此类文件或目录).我查找了其他人所做的事情,他们通过使用绝对文件路径解决了该问题.我通过以下方法进行了尝试:

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)    

进入:

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)

以下是可供参考的其余代码:

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]))

修改

制作一个空的certificate.json文件可以清除大多数错误,但我仍然遇到错误:无法识别的参数".

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

完整的错误文本:

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

推荐答案

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

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

请按照以下设置步骤正确进行身份验证.

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

第1步:打开Drive API

Step 1: Turn on the Drive API

  1. 使用此向导在Google Developers Console中创建或选择项目,然后自动打开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.

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

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

在页面顶部,选择OAuth同意屏幕标签.选择一个电子邮件地址,输入产品名称(如果尚未设置),然后单击保存"按钮.

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.

选择凭据"选项卡,单击创建凭据"按钮,然后选择OAuth客户端ID.

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

选择其他应用程序类型,输入名称"Drive API Quickstart",然后单击创建"按钮.

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

单击确定"关闭结果对话框.

Click OK to dismiss the resulting dialog.

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

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

将此文件移至您的工作目录,并将其重命名为client_secret.json.

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

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

更新

很明显,argparse和Jupyter之间存在冲突. [参考]

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

和oauth2client使用argparse Github代码链接

and oauth2client is using argparse Github Code Link

我还没有亲自使用Jupyter,但是,如果可以的话,请尝试不使用Jupyter来独立使用Google驱动器api代码.

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

这篇关于Google API:为什么找不到我的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆