Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Google Drive from a Google App Engine Python app

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.

like image 497
Lindsay Avatar asked Mar 16 '13 21:03

Lindsay


1 Answers

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.

like image 116
Takashi Matsuo Avatar answered Oct 14 '22 10:10

Takashi Matsuo