电子表格列表Gdata OAuth2

时间:2013-11-27 16:59:43

标签: gdata google-oauth google-spreadsheet-api google-apps-marketplace

在Gdata中获取带有电子表格api的电子表格列表,

Oauth1 Way

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)

但由于此won't fix issue #594

,spreadSheetService不适用于OAuth2

如何查询gdata.spreadsheets.client.SpreadsheetClient的电子表格列表?

2 个答案:

答案 0 :(得分: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()

如果您专门寻找双腿,相同的技术可行,但您需要创建不同类型的凭证对象。请参阅以下有关如何创建此问题的最新答案:Using Spreadsheet API OAuth2 with Certificate Authentication

答案 1 :(得分:4)

这是一个将OAuth 2.0 Bearer身份验证标头直接写入请求的变体,并允许您继续使用旧的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