3

在 Gdata 中使用电子表格 API 获取电子表格列表,

Oauth1 方式

spreadSheetService = gdata.spreadsheet.service.SpreadsheetsService()
spreadSheetService.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1,self.CONSUMER_KEY,self.CONSUMER_SECRET,two_legged_oauth=True, requestor_id=self.requestor_id)
spreadSheetService.GetSpreadsheetsFeed(query = q)

但是由于 spreadSheetService 不适用于 OAuth2,因此无法解决问题 #594

如何使用 查询电子表格列表gdata.spreadsheets.client.SpreadsheetClient

4

2 回答 2

7

(假设 Python)

我能够使用gd_client.auth_token = gdata.gauth.OAuth2TokenFromCredentials(credentials)由 OAuth2 流创建的凭据对象(使用 oauth2client)并将其与 gdata 库一起使用。

此处的完整示例(对于命令行应用程序):

# Do OAuth2 stuff to create credentials object
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run

storage = Storage("creds.dat")
credentials = storage.get()
if credentials is None or credentials.invalid:
  credentials = run(flow_from_clientsecrets("client_secrets.json", scope=["https://spreadsheets.google.com/feeds"]), storage)

# Use it within gdata
import gdata.spreadsheets.client
import gdata.gauth

gd_client = gdata.spreadsheets.client.SpreadsheetsClient()
gd_client.auth_token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
print gd_client.get_spreadsheets()

如果您专门寻找 2-legged,则相同的技术可以工作,但您需要创建不同类型的凭证对象。请参阅以下有关如何创建此内容的最新答案:Using Spreadsheet API OAuth2 with Certificate Authentication

于 2013-11-27T17:39:40.730 回答
4

这是一个将 OAuth 2.0 Bearer auth 标头直接写入请求并允许您继续使用旧式gdata.spreadsheet.service.SpreadsheetsService客户端代码的变体:

import httplib2
# Do OAuth2 stuff to create credentials object
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import tools

storage = Storage("creds.dat")
credentials = storage.get()
if credentials is None or credentials.invalid:
    flags = tools.argparser.parse_args(args=[])
    flow = flow_from_clientsecrets("client_secrets.json", scope=["https://spreadsheets.google.com/feeds"])
    credentials = tools.run_flow(flow, storage, flags)
if credentials.access_token_expired:
    credentials.refresh(httplib2.Http())

# Use it within old gdata
import gdata.spreadsheet.service
import gdata.service

client = gdata.spreadsheet.service.SpreadsheetsService(
    additional_headers={'Authorization' : 'Bearer %s' % credentials.access_token})

#public example
entry = client.GetSpreadsheetsFeed('0AoFkkLP2MB8kdFd4bEJ5VzR2RVdBQkVuSW91WE1zZkE') 
print entry.title
于 2015-03-20T01:34:40.923 回答