从Google App Engine Python应用访问Google云端硬盘

Lin*_*say 6 python google-app-engine google-drive-api

我有一个现有的Google App Engine Python应用程序,它具有很多功能.我现在想要将Google云端硬盘集成到应用中.具体来说,我希望我的应用能够:

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

如果有人知道如何做我想做的事情,可以将我引导到符合我的SPECIFIC要求的SPECIFIC 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")
Run Code Online (Sandbox Code Playgroud)

测试有点令人困惑,因为偶尔当我运行它时,包括第一次,它会启动访问请求页面,但大多数情况下它没有.我已经使用https://accounts.google.com/b/0/IssuedAuthSubTokens?hl=zh-CN撤消对驱动器和云端硬盘的访问权限不再显示在列表中,但我想存在一小时或更长时间的延迟用于执行访问撤销.不确定,并没有看到它记录.

在任何情况下,如果我将呼叫注释掉drive_service.files().insert(),它就不会中止,并重定向到我的概要页面.我相信这意味着授权正常工作,因为这使它像生成的示例代码一样.

但是,如果我取消评论insert和使用resumable=True媒体机构,我得到:

ResumableUploadError: Failed to retrieve starting URI.
Run Code Online (Sandbox Code Playgroud)

如果我使用resumable=False,我得到:

HttpError: <HttpError 401 when requesting https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&alt=json returned "Login Required">
Run Code Online (Sandbox Code Playgroud)

所以我似乎能够通过OAuth 2.0授权,但无法插入文件.

Tak*_*suo 1

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

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

  • 谢谢。我试过了。我还在 https://developers.google.com/drive/examples/python 上尝试了 DrEdit(我花了几个小时才意识到它与我的要求无关)。通过 https://developers.google.com/drive/quickstart-python,我观看了视频,运行了应用程序,并研究了代码,但到目前为止还没有看到如何将该信息扩展到 GAE 并删除手册流程。回到您建议的页面,我得到:“无效的参数” (2认同)
  • 这使得问题和答案对于其他人来说不太清楚。您可以恢复该问题,再次接受该问题并提出一个新问题吗? (2认同)