Accessing Spreadsheets API with OAuth2 + app_identity.get_access_token()

import pprint
import webapp2
import gdata.client
import gdata.gauth
import gdata.spreadsheets.client
from google.appengine.api import app_identity

pp = pprint.PrettyPrinter()

class GetWorksheetsHandler(webapp2.RequestHandler):
    def get(self):
        spreadsheet_key = self.request.get('key') or '0Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
        scope = 'https://spreadsheets.google.com/feeds/'
        access_token, _ = app_identity.get_access_token(scope)

        oauth2token = gdata.gauth.OAuth2Token(
            client_id='',
            client_secret='',
            scope=scope,
            access_token=access_token,
            user_agent='your_application_name'
        )

        client = gdata.spreadsheets.client.SpreadsheetsClient()
        client = oauth2token.authorize(client)

        try:
            list = client.get_worksheets(spreadsheet_key=spreadsheet_key)
            self.response.out.write(str(list))

        except gdata.client.RequestError, msg:
            logging.debug(pp.pformat(msg))

application = webapp2.WSGIApplication([
    ('/get_worksheets', GetWorksheetsHandler),
], debug=True)

You need to copy
gdata-python-client's src/{gdata,atom} directories into your AppEngine project directory.

The client acts as the user 'APP_ID@appspot.gserviceaccount.com'. When you invite it as your collaborators, the application can modify your spreadsheet.

References
Getting started with python GData spreadsheets + OAuth2
Python gdata.spreadsheets.client get_worksheets
Google App Engine > App Identity Python API Overview

コメント

このブログの人気の投稿

[linux] ping は通るのに No route to host と言われる

Chrome でダウンロードしたファイル名の一部がハイフンになる

[windows] Windows 回復環境 (WinRE) を修理する