使用Jupyter

时间:2018-05-30 19:55:52

标签: python google-api google-drive-api jupyter

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

按照步骤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

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

creds = tools.run_flow(flow, store)

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

编辑:

我设法找到了better Google tutorial。按照那里的所有步骤,我可以让脚本作为一个独立的python脚本工作。但是,从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笔记本中运行则不行。

1 个答案:

答案 0 :(得分:0)

我使用谷歌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}

Cell 2:

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

Cell 3(输出驱动器文件夹):

!ls drive

要访问驱动器,请使用:

path = 'drive/somedrivefolder'