自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用Python開發(fā)可用于iPhone的Google Reader API

開發(fā) 前端 后端
Google Reader是我最喜歡的web服務(wù),可惜在iPhone上沒有一個我理想中的客戶端,所以只好自己動手豐衣足食了。

而開發(fā)的第一步自然就是搞定Google Reader API,可惜Google一直沒有放出官方文檔。所幸的是前人已經(jīng)通過反向工程探尋出了相關(guān)信息(GoogleReaderAPI、Using the Google Reader API和GReader-Cocoa等),所以不用自己去一一摸索了。

不過文檔有點老了,這期間Google也稍微改了一些東西,所以還需要稍作修正。

由于現(xiàn)在手頭上沒有Mac,所以就不用Objective-C,而拿Python來演示。這樣代碼量也會少很多,邏輯顯得更加清晰。

在訪問之前需要證明你的身份,所以先去https://www.google.com/accounts/ClientLogin獲取登錄憑證。

此處需要POST 3個字段:service為'reader',Email為Google賬號,Passwd為密碼。此外還可以附加source字段用于標(biāo)明你的客戶端,其中網(wǎng)頁版是'scroll',iOS網(wǎng)頁版是'mobilescroll',你可以隨意改成其他字符串。

  1. from urllib import urlencode  
  2. from urllib2 import urlopen, Request  
  3.  
  4. LOGIN_URL = 'https://www.google.com/accounts/ClientLogin' 
  5. EMAIL = '你的郵箱' 
  6. PASSWORD = '你的密碼' 
  7.  
  8. request = Request(LOGIN_URL, urlencode({  
  9.     'service''reader',  
  10.     'Email': EMAIL,  
  11.     'Passwd': PASSWORD,  
  12.     'source''mobilescroll' 
  13. }))  
  14.  
  15. f = urlopen(request) 

然后會拿到這樣一串字符串,一共3行:

  1. SID=...  
  2. LSID=...  
  3. Auth=... 

這里我們需要SID和Auth,它們應(yīng)該是不會過期的,除非退出登錄:

  1. lines = f.read().split()  
  2. sid = lines[0]  
  3. auth = lines[2][5:] 

拿到這2個字段后就可以使用Google Reader API了。具體方法就是訪問API地址(見GoogleReaderAPI文檔),然后把SID作為cookie,Auth作為Authorization。

例如獲取訂閱列表:

  1. headers = {'Authorization''GoogleLogin auth=' + auth, 'Cookie': sid}  
  2. request = Request('https://www.google.com/reader/api/0/subscription/list?output=json', headers=headers)  
  3. f = urlopen(request)  
  4. print f.read() 

就會拿到如下的信息:

  1. {"subscriptions":
  2. [{"id":"feed/供稿地址","title":"供稿名","categories":
  3. [{"id":"user/Google Reader用戶ID/label/分類名","label":"分類名"}],
  4. "sortid":"不知道啥玩意","firstitemmsec":"第一個條目的時間戳","htmlUrl":"供稿的網(wǎng)站地址"},...(其他供稿的信息)]} 

不過如果要修改的話(一般是POST請求),還需要一個token參數(shù)。這個token與SID和Auth不同,它很容易過期。因此如果失效了,需要再次請求一個。

請求的地址是http[s]://www.google.com/reader/api/0/token,它可以附帶2個可選的GET參數(shù):ck是時間戳,client是客戶端名稱。

  1. request = Request('https://www.google.com/reader/api/0/token', headers=headers)  
  2. f = urlopen(request)  
  3. token = f.read() 

拿到token后就可以進(jìn)行訂閱等操作了,例如訂閱本站:

  1. request = Request('https://www.google.com/reader/api/0/subscription/quickadd?output=json', urlencode({  
  2.     'quickadd''http://www.scjtxx.cn',  
  3.     'T': token  
  4. }), headers=headers)  
  5. f = urlopen(request)  
  6. print f.read() 

會拿到這樣的結(jié)果:

  1. {"query":"http://www.keakon.net/feed","numResults":1,"streamId":"feed/http://www.scjtxx.cn"

再去看看你的Google Reader,應(yīng)該就訂閱好本站了。

原文:http://simple-is-better.com/news/565

【編輯推薦】

  1. 分享兩個Python web框架:Django&Tornado
  2. Python高手是如何練成的
  3. Python入門之你必須了解的實用技巧
  4. 如何在NetBeans 7.0安裝Python插件
  5. 使用NetBeans IDE開發(fā)Python應(yīng)用程序詳解
責(zé)任編輯:陳貽新 來源: keakon的涂鴉館
相關(guān)推薦

2009-01-04 09:16:11

google Read開發(fā)APIGoogle API

2009-08-17 08:37:31

2013-06-27 14:12:06

Google Read

2011-11-01 09:14:10

Google ReadGoogle+

2013-07-03 13:37:37

Google

2022-03-25 09:21:09

GoogleAndroid操作系統(tǒng)

2009-07-03 09:12:59

Java JSP

2021-04-13 10:32:50

ARVR蘋果

2009-07-29 16:44:45

AndroidSimpleGoogle

2023-03-03 09:09:55

2011-08-19 13:34:33

iPhone應(yīng)用ABAddressBo

2022-07-29 09:25:13

NFT可視化探索投資者

2015-05-04 13:21:56

DartAndroid

2012-07-06 13:16:37

Google Go

2011-08-08 15:05:50

iPhone 網(wǎng)站

2013-12-08 20:32:32

WaxLua

2015-04-22 09:36:27

JAVA代碼生成器

2021-03-26 11:02:20

Python自然語言API

2021-05-20 09:55:40

ChromeGoogle Read離線閱讀

2012-10-17 10:01:47

云存儲Google Play
點贊
收藏

51CTO技術(shù)棧公眾號