gpt4 book ai didi

python - 从 Jupyter 使用 Python 设置 Drive API 时出错

转载 作者:行者123 更新时间:2023-12-05 07:33:24 25 4
gpt4 key购买 nike

我正在关注 Google Quickstart Guide设置 Python 以从 Jupyter 笔记本访问 Google Drive API(最终目标是将 csv 添加到特定的 Google Drive 文件夹)。

完成步骤 1) 和 2) 后,我将添加快速入门代码。

快速启动.py

"""
Shows basic usage of the Drive v3 API.

Creates a Drive v3 API service and prints the names and ids of the last 10 files
the user has access to.
"""
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# Setup the Drive v3 API
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.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('drive', 'v3', http=creds.authorize(Http()))

# Call the Drive v3 API
results = service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print('{0} ({1})'.format(item['name'], item['id']))

运行它,我收到以下错误:

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 /Users/miguel/Library/Jupyter/runtime/kernel-dc5eae05-373c-42a9-a2da-86c096ca8330.json

我用谷歌搜索并在这里找到了一些东西(包括 this 聊天,这似乎是一个类似的问题)但我无法解决问题。注释掉部分代码后,似乎抛出错误的行是

creds = tools.run_flow(flow, store)

知道如何解决这个问题吗?

编辑:

我设法找到了 better Google tutorial跟随。按照那里的所有步骤,我可以让脚本作为独立的 python 脚本运行。但是,当从 Jupyter notebook 运行时它仍然不起作用。

使用的代码与上一个类似。

from __future__ import print_function

from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/drive.readonly.metadata'
store = file.Storage('storage.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)
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))

files = DRIVE.files().list().execute().get('files', [])
for f in files:
print(f['name'], f['mimeType'])

抛出错误的行是相同的:

creds = tools.run_flow(flow, store)

返回的错误是一样的:

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 /Users/miguel/Library/Jupyter/runtime/kernel-a0ff6ecb-3947-41be-be03-d2d96776fc8e.json

为清楚起见:此编辑的主要内容是代码在独立脚本中运行,但在 Jupyter 笔记本中运行时则不然。

最佳答案

我正在使用 google colab 并添加一个驱动器是这 3 个单元格:

单元格 1:

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse

from google.colab import authi
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass

!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

单元格 2:

!mkdir -p drive
!google-drive-ocamlfuse drive

单元格 3(输出驱动器文件夹):

!ls drive

要访问驱动器,请使用:

path = 'drive/somedrivefolder'

关于python - 从 Jupyter 使用 Python 设置 Drive API 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50612508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com