Python应用访问Google

Python应用访问Google

本文介绍了从Google App Engine Python应用访问Google Drive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的Google App Engine Python应用程序,它具有很多功能。我现在想要将Google云端硬盘整合到应用中。具体而言,我希望我的应用能够:


  1. 在用户的Google云端硬盘中创建一个空文件,供用户创建Google文档。

  2. 从Google云端硬盘中检索该文件,以便在我的应用中进一步处理。

  3. 定期将其发送回Google Drive,以便用户可以进一步编辑作为Google Doc。

如果有人知道如何去做我想做的事情,我会永远感激不尽可以引导我访问特定Google网页,以满足我的特定要求(不是一般性回答,例如请参阅DrEdit示例)。提前致谢!



更新: 根据回答1中的建议,在 drive-v2-python-appengine 中生成的示例代码,这里是我的带有RequestHandler的程序,用于创建一个空文件:

 进口os 
进口webapp2

从google.appengine进口io

.api导入memcache

从apiclient.discovery导入httplib2
导入从apiclient.http导入生成
从oauth2client.appengine导入MediaIoBaseUpload
导入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)


类CreateEmptyFile(webapp2.RequestHandler):
@decorato r.oauth_required
def get(self):
body = {
'title':'示例文档',
'description':'示例文档',
'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)

测试有点令人困惑,因为偶尔当我运行它时(包括第一次),它会出现访问请求页面,但大多数情况下不会。我已使用撤消对云端硬盘和云端硬盘的访问权限不再显示在列表中,但我认为存在一小时或更长时间的延迟来执行访问撤消。不知道,并没有看到它的记录。



无论如何,如果我注释掉对 drive_service.files的调用( ).insert(),它不会中止,并重定向到我的摘要页面。我相信这意味着授权工作正常,因为它使它像生成的示例代码一样。



但是,如果我取消注释插入并使用 resumable = True 作为媒体正文,我得到:

  ResumableUploadError:无法检索起始URI。 

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

  HttpError:< HttpError 401在请求https://www.googleapis.com/upload/drive/v2时/ files?uploadType = multipart& alt = json返回需要登录> 

因此,我似乎可以通过OAuth 2.0授权,但无法插入文件。 / p>

解决方案

请尝试我们的快速入门应用程序:



您可以创建一个快速启动应用程序引擎应用程序,这对您可以创建初始设置很有用。有关特定用例的信息,请参阅。


I have an existing Google App Engine Python app with a lot of functionality. I now want to integrate Google Drive into the app. 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.
  2. Retrieve that file from Google Drive for further processing in my app.
  3. Send it back to Google Drive periodically so that the user can perform further editing on it as a Google Doc.

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"). 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:

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. 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. 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:

ResumableUploadError: Failed to retrieve starting URI.

And if I use resumable=False, I get:

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.

解决方案

Please try our quickstart app at: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.

这篇关于从Google App Engine Python应用访问Google Drive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 16:33