Zend Google Data Spreadsheets with OAuth 2

I had the privilege of figuring out why our systems were having issues connecting to the Google Spreadsheet service. As of a couple of months ago it seems like Google deprecated one of their authentication systems and we were not made aware.

One of our old projects was using the Zend Framework 1 along with its Zend GData library to connect to Google’s spreadsheet service and manipulate spreadsheets in Google Docs. One day this mysteriously stopped working. It turns out that they disabled the client auth system and began forcing users to migrate to OAuth 2.

The worst part about this is that Zend GData did not support OAuth 2. I had the tedious task of going through and hacking together a solution. It turns out that we were also going to be deprecating this system soon, so all I needed to do was get it working for the short term.

To do this, I started by examining the headers send by the previous client auth system and I compared it to the headers in this post. From there it was just a matter of making sure to send the correct header so that Google would authenticate the request.

The first thing you need to do is get an OAuth token from Google. You can do this using the Google API PHP Client. The documentation has examples on how to get your token. Once you have that you need to pass it through Zend GData when making calls.

Getting your OAuth 2 keys and setting up

Use the token in the Zend GData library

We need to update two files so that the calls to the Google service will be authenticated. First, look for file /Zend/Gdata/App.php.

At about line 648 add this:

Here is an example with surrounding code so you know what to look for incase the line numbers in your version of the library are not the same as mine.

The other file we need to update is /Zend/Oauth/Client.php.

At about line 248 you want to stop using the original Authorization header and then include our own instead.

I urge you to reorganize this so that you only comment out the original Authorization header when our OAUTH_AUTHORIZATION_HEADER is set.