如何使用Python客户端/发现服务通过Google Spreadsheets API进行身份验证? [英] How to authenticate with Google Spreadsheets API using Python client / discovery service?

查看:115
本文介绍了如何使用Python客户端/发现服务通过Google Spreadsheets API进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 2.7和Google API的客户端库,我正尝试使用它来对电子表格进行身份验证访问:

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:

文件"/Library/Python/2.7/site-packages/apiclient/discovery.py",行 196,建造中 版本))apiclient.errors.UnknownApiNameOrVersion:名称:电子表格版本: v2

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

我知道我做错了,但是在不使用ClientLogin和/或.NET/Java客户端库的情况下,我找不到查找身份验证示例的麻烦.

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

推荐答案

旧的Python gdata服务库支持ClientLogin,AuthSub和OAuth 1.0身份验证.所有这些都已弃用.如果您希望使用OAuth 2.0服务帐户凭据,则需要一起破解一些东西,例如:

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

这篇关于如何使用Python客户端/发现服务通过Google Spreadsheets API进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆