我有一个现有的Google App Engine Python应用程序,它具有很多功能。我现在想要将Google云端硬盘整合到应用中。具体来说,我想我的应用程序能够:从Google App Engine访问Google Drive Python应用程序
- 创建我的用户的谷歌云端硬盘上的空文件,其中我的用户可以创建一个谷歌文档。
- 从Google云端硬盘中检索该文件,以便在我的应用中进一步处理。
- 定期将其发送回Google Drive,以便用户可以将其作为Google文档进行进一步编辑。
如果有人知道如何做我想做的事情,可以将我引导到特定的Google网页,以解决我的特定需求(不是一般性回答,如“请参阅DrEdit示例“)。提前致谢!
更新:
基于对回答1 drive-v2-python-appengine
每建议生成的示例代码,这是我的计划有RequestHandler创建一个空文件:
import os
import webapp2
import io
from google.appengine.api import memcache
import httplib2
from apiclient.discovery import build
from apiclient.http import MediaIoBaseUpload
from oauth2client.appengine import oauth2decorator_from_clientsecrets
decorator = oauth2decorator_from_clientsecrets(
os.path.join(os.path.dirname(__file__), 'client_secrets.json'),
scope=[
'https://www.googleapis.com/auth/drive',
])
http = httplib2.Http(memcache)
drive_service = build("drive", "v2", http=http)
class CreateEmptyFile(webapp2.RequestHandler):
@decorator.oauth_required
def get(self):
body = {
'title': 'Sample Document',
'description': 'A sample document',
'mimeType': 'text/plain'
}
media_body = MediaIoBaseUpload(io.BytesIO(""), mimetype='text/plain', resumable=True)
file = drive_service.files().insert(body=body, media_body=media_body).execute()
self.redirect("/synopsis")
测试是有些令人困惑,因为偶尔当我运行这个(包括第一次)时,会出现访问请求页面,但大多数情况下不会。我已使用https://accounts.google.com/b/0/IssuedAuthSubTokens?hl=en撤消对Drive and Drive的访问权限,不再显示在列表中,但我认为存在一小时或更长时间的延迟来执行访问撤消。不确定,并没有看到它的记录。
在任何情况下,如果我将对drive_service.files().insert()
的呼叫注释掉,它不会中止并重定向到我的摘要页面。我相信这意味着授权正常工作,因为这使得它像生成的示例代码一样。
但是,如果我取消注释insert
和使用resumable=True
为媒体身上,我得到:
ResumableUploadError: Failed to retrieve starting URI.
如果我使用resumable=False
,我得到:
HttpError: <HttpError 401 when requesting https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&alt=json returned "Login Required">
所以,我似乎能够通过OAuth 2.0授权,但不能插入文件。
欢迎来到StackOverflow!请考虑重写您的问题并删除一些与回答无关的段落(如第4和第5段)。试着具体一点,因为我必须阅读两遍才能理解你想要的内容,这个问题的范围似乎非常广泛。 – 2013-03-16 21:33:53
对不便和感谢您的欢迎和指导。希望我的编辑版本更好。范围并不是很宽泛:我只希望我的应用程序能够将文档来回发送到Google云端硬盘,以便用户可以使用Google文档用户界面编辑文档,但我也可以在我的应用中使用文档。 – Lindsay 2013-03-16 21:45:06
我喜欢这个问题 - 你在哪里找到“drive-v2-python-appengine”?我无法在网上找到它。我不喜欢DrEdit,因为它依赖大量混淆的javascript来运行。寻找更直接的例子。 – slashdottir 2014-01-31 16:31:20