A few weeks ago, I published a tutorial regarding using the Google Reader API in PHP projects.  Since that time, Google has changed the authentication portion of their API somewhat, so I need to revise my instructions just a bit.  And some day in the future, I'll even post a tutorial on how to use oAuth with the Google API as well.

Initially, you need to request an SID from Google.  You pass your Google username and password in a standard HTTP POST and the response contains a string that looks something like this:


You see three named keys there: SID, LSID, and Auth.  In the old days, all you needed was the SID to create an authentication token.  Then you passed this token in a cookie with every POST or GET request to the API.  Though I always wondered why Google generated those other values if they weren't used.  Failing to use them, though, broke my PHP class for connecting to Google Reader ... and upset quite a few users of my WordPress plug-in.

After some research, I found that Google now wants you to send the Auth key in the headers of your requests.  Instead of setting the cookie as a cURL variable, you add an HTTP header "Authorization: GoogleLogin auth=" with your Auth key.  Here's the new _getToken() method from my Google Reader class:

[cc lang="php"]
private function _getToken() {
$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_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded', 'Authorization: GoogleLogin auth=' . $this->_auth));
curl_setopt($ch, CURLOPT_URL, $url);



$this->_token = ob_get_contents();


Compare this with the _getToken() function from my last tutorial.

The class itself now sets this header with every HTTP GET and POST request before passing them to the server rather than setting the cookie like we did last time.  For the time being, this new method works.  But I'm still working on an oAuth version to take its place for the long run.