首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从Jupyter使用Python设置Drive API时出错

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

Stack Overflow用户
提问于 2018-05-31 03:55:52
回答 1查看 634关注 0票数 3

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

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

quickstart.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 chat,它似乎是一个类似的问题),但我不能解决这个问题。在注释掉部分代码之后,似乎抛出错误的代码行是

creds = tools.run_flow(flow, store)

你知道怎么解决这个问题吗?

编辑:

我设法找到了一个可以关注的better Google tutorial。按照所有的步骤,我可以让这个脚本像一个独立的脚本一样工作。但是,当它在Jupyter笔记本上运行时,它仍然无法工作。

使用的代码类似于前面的代码。

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 notebook中运行。

EN

回答 1

Stack Overflow用户

发布于 2018-05-31 08:22:49

我正在使用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'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50612508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档