26
votes

Does Google Reader have an API and if so, how can I get the count of the number of unread posts for a specific user knowing their username and password?

4
@GateKiller: I understand the motivation for throwing a "code-request" tag onto this... I also have an open bounty on a question where the only answer is a link to the (broken) API documentation, and it irritates me that i'll end up paying out 300 rep to someone who just Googled my keywords. - Shog9
But, the tag doesn't serve any purpose. Rather, you should add a note to your actual question stating you'd appreciate sample code. - Shog9

4 Answers

45
votes

This URL will give you a count of unread posts per feed. You can then iterate over the feeds and sum up the counts.

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

Here is a minimalist example in Python...parsing the xml/json and summing the counts is left as an exercise for the reader:

import urllib
import urllib2

username = '[email protected]'
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

And some additional links on the topic:

11
votes

It is there. Still in Beta though.

6
votes

Here is an update to this answer

import urllib
import urllib2

username = '[email protected]'
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 Reader removed SID auth around June, 2010 (I think), using new Auth from ClientLogin is the new way and it's a bit simpler (header is shorter). You will have to add service in data for requesting Auth, I noticed no Auth returned if you don't send the service=reader.

You can read more about the change of authentication method in this thread.

0
votes

In the API posted in [1], the "token" field should be "T"

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