I'm trying to create a GAE app in which a user can visit the appspot domain, be authorized (or not) using OAuth2 and then have one of their Google spreadsheets modified in an automated way using gdata.spreadsheet.service
. I've gotten this to work using SignedJwtAssertionCredentials
, but in that case the user must specifically allow editing from the app; I'm trying to skip this step, and have the app modify the user's spreadsheet from their own account using OAuth2.
The documentation provided by Google says that decorators are the easiest way to accomplish this, doing something like this:
from apiclient.discovery import build
from google.appengine.ext import webapp
from oauth2client.appengine import OAuth2Decorator
decorator = OAuth2Decorator(
client_id='your_client_id',
client_secret='your_client_secret',
scope='https://www.googleapis.com/auth/calendar')
service = build('calendar', 'v3')
...
@decorator.oauth_required
def get(self):
# Get the authorized Http object created by the decorator.
http = decorator.http()
# Call the service using the authorized Http object.
request = service.events().list(calendarId='primary')
response = request.execute(http=http)
but I have no idea what to the do with this service
object to accomplish my goal with the spreadsheet modification. Any general tips or specific tips on how to work with the service
object would be helpful.
In the provided example you're constructing a calendar service using Google Calendar API, which is not GData based API. For GData based API's you'll have to use
gdata.gauth
instead.Please note that
gdata.spreadsheet.service
will not work withgdata.gauth
as it only supports deprecated ClientLogin (check theSpreadsheetsService
constructor available at [1]). You should usegdata.spreadsheets.client
instead.Complete
SpreadsheetsClient
documentation is available at [2]. You may consider this example where a worksheet is added to the spreadsheet:And regarding OAuth, I would use OAuth2WebServerFlow instead (See [3] for more information). Credentials object can be serialized and deserialized with pickle. Even easier way to store credentials object is described at [4].
[1] - https://code.google.com/p/gdata-python-client/source/browse/src/gdata/spreadsheet/service.py?r=f7a9cb244df430d960f6187ee0fbf85fe0218aac
[2] - https://gdata-python-client.googlecode.com/hg/pydocs/gdata.spreadsheets.client.html#SpreadsheetsClient
[3] - https://developers.google.com/api-client-library/python/guide/aaa_oauth#OAuth2WebServerFlow
[4] - https://developers.google.com/api-client-library/python/guide/google_app_engine#Credentials