3

掌握在GDATA電子表格API電子表格的列表,清單電子表格GDATA的OAuth2

您好!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不可用,因爲這個won't fix issue #594

如何做的OAuth2我用gdata.spreadsheets.client.SpreadsheetClient查詢電子表格列表?

回答

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條腿,同樣的技術工作,但你需要創建一個不同類型的憑據目的。見關於如何創建這個在最近的答案:Using Spreadsheet API OAuth2 with Certificate Authentication

+0

謝謝,我還發現你可以通過傳遞一個實例'SpreadSheetQuery'作爲命名參數'query'來查詢特定的電子表格 – Gautam

4

下面是直接寫入一個OAuth 2.0承載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 
相關問題