简体   繁体   English

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

[英]Accessing Google Drive from a Google App Engine Python app

I have an existing Google App Engine Python app with a lot of functionality. 我有一个现有的Google App Engine Python应用程序,它具有很多功能。 I now want to integrate Google Drive into the app. 我现在想要将Google云端硬盘集成到应用中。 Specifically I want my app to be able to: 具体来说,我希望我的应用能够:

  1. Create an empty file in my user's Google Drive where my user can create a Google Doc. 在我的用户的Google云端硬盘中创建一个空文件,我的用户可以在其中创建Google文档。
  2. Retrieve that file from Google Drive for further processing in my app. 从Google云端硬盘中检索该文件,以便在我的应用中进一步处理。
  3. Send it back to Google Drive periodically so that the user can perform further editing on it as a Google Doc. 定期将其发送回Google云端硬盘,以便用户可以将其作为Google文档进行进一步编辑。

I'd be eternally grateful if someone who knows how to do what I'm trying to do can direct me to the SPECIFIC Google webpage(s) that address my SPECIFIC requirement (not a general answer like, "See the DrEdit example"). 如果有人知道如何做我想做的事情,可以将我引导到符合我的SPECIFIC要求的SPECIFIC Google网页(不是像“DrEdit示例”这样的一般答案),我将永远感激不尽。 。 Thanks in advance! 提前致谢!

Update: 更新:

Based on the generated sample code in drive-v2-python-appengine per the suggestion in Answer 1, here's my program with a RequestHandler for creating an empty file: 根据在答案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")

Testing is somewhat confusing, because occasionally when I've run this, including the first time, it's brought up the access request page, but most of the time it doesn't. 测试有点令人困惑,因为偶尔当我运行它时,包括第一次,它会启动访问请求页面,但大多数情况下它没有。 I've used https://accounts.google.com/b/0/IssuedAuthSubTokens?hl=en to revoke access to Drive and Drive no longer shows up on the list, but I guess a time delay of an hour or more exists for carrying out the access revocation. 我已经使用https://accounts.google.com/b/0/IssuedAuthSubTokens?hl=zh-CN撤消对驱动器和云端硬盘的访问权限不再显示在列表中,但我想存在一小时或更长时间的延迟用于执行访问撤销。 Not sure about that, and haven't seen it documented. 不确定,并没有看到它记录。

In any case, if I comment-out the call to drive_service.files().insert() , it does not abort, and redirects to my synopsis page. 在任何情况下,如果我注释掉对drive_service.files().insert()的调用,它不会中止,并重定向到我的概要页面。 I believe this means the authorization is working correctly, since that makes it like the generated sample code. 我相信这意味着授权正常工作,因为这使它像生成的示例代码一样。

However, if I un-comment the insert and use resumable=True for the media body, I get: 但是,如果我取消注释insert并对媒体体使用resumable=True ,我得到:

ResumableUploadError: Failed to retrieve starting URI.

And if I use resumable=False , I get: 如果我使用resumable=False ,我得到:

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

So I seem to be able to get thru the OAuth 2.0 authorization, but cannot insert a file. 所以我似乎能够通过OAuth 2.0授权,但无法插入文件。

Please try our quickstart app at: https://developers.google.com/api-client-library/python/start/installation 请尝试我们的快速入门应用: https//developers.google.com/api-client-library/python/start/installation

You can create a quickstart app engine app, which is useful for you to create the initial setup. 您可以创建快速入门应用程序引擎应用程序,这对您创建初始设置很有用。 For specific use-cases, please refer to the drive API reference . 有关特定用例,请参阅驱动器API参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM