Note on 8 February 2017: Google has released the Sheets API v4 and is now providing PHP sample code and improved PHP support for Sheets in the PHP API client.  My series of articles is now largely obsolete.  See https://developers.google.com/sheets/api/quickstart/php
==========================
Google has been sending out messages saying that several of their APIs are going to be discontinued (for example, the Document List API). Â As a result, I tried to modify my custom Google Spreadsheet-populating PHP script to use the new Drive API. Â After way too much wasted time, I discovered that the Drive API can create sheets and read their metadata, but can’t add rows — for that we can keep using the Spreadsheets API.
I was successful at converting the authentication portion of my script from the old ClientLogin to OAuth with a Service Account (the script runs behind the scenes of a web site and populates a Google spreadsheet that the web site user has no knowledge of).
It wasn’t as simple as I’d hoped because the Google-provided PHP client for the Drive API doesn’t know about the Spreadsheets API. Â I had to dig around the PHP libraries to piece together authentication codes and Google_Http_Requests. Â Then Google changed the PHP client to use Guzzle, so I recently made that change.
So, if anyone else out there is interested in the combination of PHP, the Spreadsheets API, and OAuth with Service Accounts, this could save you some time.
The code is available in a public Gist at https://gist.github.com/karlkranich/de225928665dc6b83667
Here are the step-by-step instructions to get this to work:
Continue reading →