Google Reader API Unread Count

时间:2008-09-09 20:53:57

标签: api google-reader

Google阅读器是否有API?如果是这样,我如何知道特定用户知道其用户名和密码的未读帖子数?

4 个答案:

答案 0 :(得分:45)

此网址会为您提供每个Feed的未读帖子数。然后,您可以遍历Feed并总结计数。

http://www.google.com/reader/api/0/unread-count?all=true

这是Python中的一个极简主义示例...解析xml / json并将计数求和留作读者的练习:

import urllib
import urllib2

username = 'username@gmail.com'
password = '******'

# Authenticate to obtain SID
auth_url = 'https://www.google.com/accounts/ClientLogin'
auth_req_data = urllib.urlencode({'Email': username,
                                  'Passwd': password,
                                  'service': 'reader'})
auth_req = urllib2.Request(auth_url, data=auth_req_data)
auth_resp = urllib2.urlopen(auth_req)
auth_resp_content = auth_resp.read()
auth_resp_dict = dict(x.split('=') for x in auth_resp_content.split('\n') if x)
auth_token = auth_resp_dict["Auth"]

# Create a cookie in the header using the SID 
header = {}
header['Authorization'] = 'GoogleLogin auth=%s' % auth_token

reader_base_url = 'http://www.google.com/reader/api/0/unread-count?%s'
reader_req_data = urllib.urlencode({'all': 'true',
                                    'output': 'xml'})
reader_url = reader_base_url % (reader_req_data)
reader_req = urllib2.Request(reader_url, None, header)
reader_resp = urllib2.urlopen(reader_req)
reader_resp_content = reader_resp.read()

print reader_resp_content

关于该主题的一些其他链接:

答案 1 :(得分:11)

there。尽管如此仍处于测试阶段。

答案 2 :(得分:6)

以下是对this answer

的更新
import urllib
import urllib2

username = 'username@gmail.com'
password = '******'

# Authenticate to obtain Auth
auth_url = 'https://www.google.com/accounts/ClientLogin'
#auth_req_data = urllib.urlencode({'Email': username,
#                                  'Passwd': password})
auth_req_data = urllib.urlencode({'Email': username,
                                  'Passwd': password,
                                  'service': 'reader'})
auth_req = urllib2.Request(auth_url, data=auth_req_data)
auth_resp = urllib2.urlopen(auth_req)
auth_resp_content = auth_resp.read()
auth_resp_dict = dict(x.split('=') for x in auth_resp_content.split('\n') if x)
# SID = auth_resp_dict["SID"]
AUTH = auth_resp_dict["Auth"]

# Create a cookie in the header using the Auth
header = {}
#header['Cookie'] = 'Name=SID;SID=%s;Domain=.google.com;Path=/;Expires=160000000000' % SID
header['Authorization'] = 'GoogleLogin auth=%s' % AUTH

reader_base_url = 'http://www.google.com/reader/api/0/unread-count?%s'
reader_req_data = urllib.urlencode({'all': 'true',
                                    'output': 'xml'})
reader_url = reader_base_url % (reader_req_data)
reader_req = urllib2.Request(reader_url, None, header)
reader_resp = urllib2.urlopen(reader_req)
reader_resp_content = reader_resp.read()

print reader_resp_content

Google阅读器在2010年6月左右删除了SID身份验证(我认为),使用来自ClientLogin的新Auth是一种新方式,它更简单(标头更短)。您必须在请求service的数据中添加Auth,如果您不发送Auth,我发现没有service=reader返回。

您可以在this thread中了解有关更改身份验证方法的详情。

答案 3 :(得分:0)

在[1]中发布​​的API中,“token”字段应为“T”

[1] http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI