0

I'm using Python 2.7 and the client library for Google API which I am trying to use to get authenticated access to Spreadsheets like so:

# sa == Service Account
scope = 'https://spreadsheets.google.com/feeds'
credentials = SignedJwtAssertionCredentials(sa_id, sa_key, scope)
http = httplib2.Http()
http = credentials.authorize(http)
build('spreadsheets', 'v2', http=http)

Note this is from a client script and not in Google App Engine. The output of the above is:

File "/Library/Python/2.7/site-packages/apiclient/discovery.py", line 196, in build version)) apiclient.errors.UnknownApiNameOrVersion: name: spreadsheets version: v2

I know I'm doing this wrong, but I'm having trouble finding any examples of authenticating without using ClientLogin and/or the .NET/Java client libraries.

[UPDATE] The answer may be in the following source example, but I noticed on skimming it that it still uses email/password: https://code.google.com/p/gdata-python-client/source/browse/src/gdata/spreadsheet/service.py

Community
  • 1
  • 1
Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112
  • Did you read the big red box at the top of your second link? The one that says that api versions 1 and 2 are no longer available? – Daniel Roseman Jan 30 '14 at 21:16

1 Answers1

2

The old Python gdata service libraries support ClientLogin, AuthSub and OAuth 1.0 authentication. All of which have been deprecated. If you wish to use the OAuth 2.0 Service Account credentials you'll need to hack something together like:

def buildSpreadsheetService():
  scope = 'https://spreadsheets.google.com/feeds'
  credentials = SignedJwtAssertionCredentials(sa_id, sa_key, scope)
  http = httplib2.Http()
  http = credentials.authorize(http)
  build('drive', 'v2', http=http)
  sheets = gdata.spreadsheet.service.SpreadsheetsService()
  sheets.additional_headers = {'Authorization': 'Bearer %s' % http.request.credentials.access_token}
  return sheets
Jay Lee
  • 13,415
  • 3
  • 28
  • 59
  • Hello again, Jay! Unfortunately without the call to `build()`, the `http.request.credentials.access_token` is `None`. My question is specifically around that line because once I get past it I can then pass that `http` object as per the other question you answered (http://stackoverflow.com/questions/21420777/how-would-one-convert-wrap-a-httplib2-instance-as-a-session). – Neil C. Obremski Jan 30 '14 at 21:55
  • edited to add build('drive', 'v2', http=http) which should leave http with a valid access token. – Jay Lee Jan 30 '14 at 21:59
  • I tried this and am getting a 403 Access Denied response when I try to update a cell. I've enabled Drive API for the app. Any ideas about why this could be? – Ryan Sep 14 '14 at 17:59