6

我有一个现有的具有很多功能的 Google App Engine Python 应用程序。我现在想将 Google Drive 集成到应用程序中。具体来说,我希望我的应用能够:

  1. 在我的用户的 Google Drive 中创建一个空文件,我的用户可以在其中创建一个 Google Doc。
  2. 从 Google Drive 中检索该文件以在我的应用程序中进行进一步处理。
  3. 定期将其发送回 Google Drive,以便用户可以将其作为 Google Doc 进行进一步编辑。

如果知道如何做我想做的事情的人可以将我引导到满足我的特定要求的特定 Google 网页,我将永远感激不尽(不是一般的答案,例如“请参阅 DrEdit 示例”) . 提前致谢!

更新:

根据drive-v2-python-appengine答案 1 中的建议生成的示例代码,这是我的带有 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_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 授权,但无法插入文件。

4

1 回答 1

1

请尝试我们的快速入门应用程序: https ://developers.google.com/api-client-library/python/start/installation

您可以创建一个快速启动应用程序引擎应用程序,这对您创建初始设置很有用。具体用例请参考驱动 API 参考

于 2013-03-17T07:47:24.803 回答