Google Reader API – A Brief Tutorial
A few days ago, I asked a question on Stack Overflow regarding the Google Reader API. What I wanted to do was build a WordPress plug-in that would import my feeds from Google Reader into my dashboard. I’ve already pulled in my site statistics and Facebook, Google Reader is the last holdout keeping me from managing all of my online stuff in one spot.
Unfortunately, the best tutorial I could find online (and the one recommended by Stack Overflow users) was written for C# … WordPress runs in PHP. The two languages might look the same, but it would be like trying to put a chunk of Shakespearean writing in the middle of a modern book – out of context things would just break down. So I set out to build my own version of a Google Reader reader in PHP. Here’s how to do it:
Step 1: Get an SID and Token
Google Reader authenticates users based on their username (Gmail address) and password. After that, the system gives you an SID, which you can in turn use to get a token. All of these elements work together to make sure you are really you and have permission to read, modify, and delete items from your Google Reader feed. Here’s the code you use to get an SID and token:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | class JDMReader { private $_username; private $_password; private $_sid; private $_token; private $_cookie; public function __construct($username, $password) { $this->_username = $username; $this->_password = $password; $this->_connect(); } private function _connect() { $this->_getToken(); return $this->_token != null; } private function _getToken() { $this->_getSID(); $this->_cookie = "SID=" . $this->_sid . "; domain=.google.com; path=/"; $url = "http://www.google.com/reader/api/0/token"; $ch = curl_init(); curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie); curl_setopt($ch, CURLOPT_URL, $url); ob_start(); curl_exec($ch); curl_close($ch); $this->_token = ob_get_contents(); ob_end_clean(); } private function _getSID() { $requestUrl = "https://www.google.com/accounts/ClientLogin?service=reader&Email=" . urlencode($this->_username) . '&Passwd=' . urlencode($this->_password); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $requestUrl); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); ob_start(); curl_exec($ch); curl_close($ch); $data = ob_get_contents(); ob_end_clean(); $sidIndex = strpos($data, "SID=")+4; $lsidIndex = strpos($data, "LSID=")-5; $this->_sid = substr($data, $sidIndex, $lsidIndex); } } |
This set of functions forms the core of a new PHP class I’ve built for my Google Reader plug-in. When you create a new instance of the class, you pass in your Google username and password. From that, the class will get an SID, get a token, and create a cookie string that it will pass to the server every time you make a subsequent request. It’s a fairly simple system, and translates Martin Doms’ C# approach into PHP as closely as possible.
I’ve used this already to get my Google Reader feed. It works quickly and effectively. Now I just need to wrap it all in a nice widget and share it with the rest of the world. All of the power of Google Reader, right in your WordPress dashboard!
Step 2: Try it Out!
The majority of API calls you can make involve a GET request, so I built a helper function into my class to handle those calls:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | private function _httpGet($requestUrl, $getArgs) { $url = sprintf('%1$s?%2$s', $requestUrl, $getArgs); $https = strpos($requestUrl, "https://"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if($https === true) curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie); ob_start(); try { curl_exec($ch); curl_close($ch); $data = ob_get_contents(); ob_end_clean(); } catch(Exception $err) { $data = null; } return $data; } |
This function works well enough, but unless you have something to get with your GET, it’s pretty useless. So I added a public method to my class that returns a list of all my subscriptions in a JSON array:
1 2 3 4 5 6 |
Now you’re off and running. Two lines of code will print out your reading list:
1 2 | $reader = new Reader('myname@gmail.com', 'mypassword'); echo $reader->listAll(); |
Easy, isn’t it? Now it’s your turn to try things out …
As it turns out, Google has changed the authentication portion of the Reader API. Now, instead of passing the SID in a cookie when you make a request, you set an authentication header with the “Auth” key originally passed with the SID.
I’ll publish an updated tutorial soon, but for now, I recommend turning to the comments on Martin Doms’ original article: http://blog.martindoms.com/2010/01/20/using-the-google-reader-api-part-3/#comment-105
A C# implementation of the changed authentication: http://sandrinodimattia.net/blog/post/Consuming-Google-(Reader)-with-NET-Part-1-Authentication.aspx
Hi Eric, thanks a lot for your work on this. On my webhost I can only run php, so Google API accessing using this would be incredibly helpful. I hate being unable to filter my starred reader items (e.g. according to folders) and plan to write this functionality myself, once I can access the API. I was wondering if I might expect any updates concerning an updated tutorial soon. Best regards
J, An update to the tutorial will actually be posted here today at 8am Pacific time. You just beat me by a few hours!
In the interest of keeping open source open, I also plan to publish the entire greader class library to Google Code later today. This way others can contribute to it as well.
I just posted the library on Google Code to encourage feedback and collaboration. Feel free to take a look at the code, suggest changes, etc: http://code.google.com/p/greader-library/