views:

7103

answers:

4

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?

+11  A: 

It is there. Still in Beta though.

Gulzar
This question answers the question as it is asked. If there is some reason why the question author does not find it sufficient, perhaps he should edit his question to clarify.
EBGreen
A lot of the information on that site is outdated.
Martin Doms
As much as I would like it, this is NOT an official Google Reader api. It is merely reverse-engineered guess that can break at any moment.
drozzy
+23  A: 

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:

jimmyorr
For general interst - since the changes made to Google Reader API in june, this example no longer works...
Joe Reddington
At the time being, August 9, 2010, this no longer works.
Daniel Ribeiro
Fixed this example. Thanks to livibetter -- I had read up on the SID -> Auth change, but didn't see the 'service': 'reader' part documented anywhere.
jimmyorr
Daniel Ribeiro
A: 

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

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

Yassin
This is not a scientific article. You can place links [1] inline! :-)
drozzy
+3  A: 

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.

livibetter