All I\'m trying to do is read a Google Spreadsheet from a web site. I\'ve read and re-read the Google Drive API docs and everything Google Drive PHP on Stack Overflow and I
You need to use oauth, if you don't google will only allow you to make a few requests.
If all you want to do is read data out of a google spreadsheet or write data into it then you can just use the spreadsheet api. Check out php-google-spreadsheet-client.
There is also a much easier, but less clean solution, if you don't want to bother with the API or Google Authentication.
You can now access the contents like any other csv File on the Web. Here is some sample code:
$spreadsheet_url="https://docs.google.com/spreadsheet/pub?key=<somecode>&single=true&gid=0&output=csv";
if(!ini_set('default_socket_timeout', 15)) echo "<!-- unable to change socket timeout -->";
if (($handle = fopen($spreadsheet_url, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$spreadsheet_data[] = $data;
}
fclose($handle);
}
else
die("Problem reading csv");
If you want you own file to be read you need a service account instead of a "Client ID for web applications". I've been battling this problem myself for way to long and this brought me the sollution: https://developers.google.com/drive/web/service-accounts
I created a sample project that uses a service account to authenticate against Google Spreadsheets in order to access to the contents of a spreadsheet.
Have a look at the README at https://github.com/juampynr/google-spreadsheet-reader.
Please check the Google Drive PHP Quickstart. You have not actually authorized your client. Starting from $authUrl = $client->createAuthUrl();
All Google Drive requests need authorization of some kind.