Google Reader API Unread Count

后端 未结 4 1882
抹茶落季
抹茶落季 2020-12-07 07:53

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条回答
  •  抹茶落季
    2020-12-07 08:24

    Here is an update to 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 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.

提交回复
热议问题