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 still can't get to the end zone.
Here's what I've done :
<?php session_start(); require_once 'lib/gapi/Google_Client.php'; require_once 'lib/gapi/contrib/Google_DriveService.php'; define( 'GDRIVE_CLIENT_ID', '<API Console - API Access - Client ID>' ); define( 'GDRIVE_CLIENT_SECRET', '<API Console - API Access - Client secret>' ); define( 'GDRIVE_REDIRECT_URIS', '<API Console - API Access - Redirect URIs>' ); define( 'GDRIVE_SCOPE_01', 'h t t p s://www.googleapis.com/auth/drive' ); define( 'GDRIVE_SCOPE_02', 'h t t p s://www.googleapis.com/auth/drive.apps.readonly' ); define( 'GDRIVE_SCOPE_03', 'h t t p s://www.googleapis.com/auth/drive.file' ); define( 'GDRIVE_SCOPE_04', 'h t t p s://www.googleapis.com/auth/drive.metadata.readonly' ); define( 'GDRIVE_SCOPE_05', 'h t t p s://www.googleapis.com/auth/drive.readonly' ); define( 'GDRIVE_FILE_KEY', '<'key' given from 'sharing' document>' ); $client = new Google_Client(); $client->setClientId( GDRIVE_CLIENT_ID ); $client->setClientSecret( GDRIVE_CLIENT_SECRET ); $client->setRedirectUri( GDRIVE_REDIRECT_URIS ); $client->setScopes( array( GDRIVE_SCOPE_01, GDRIVE_SCOPE_02, GDRIVE_SCOPE_03, GDRIVE_SCOPE_04, GDRIVE_SCOPE_05 ) ); try { $file = $service->files->get( GDRIVE_FILE_KEY ); echo "Title: ", $file->getTitle(); echo "Description: ", $file->getDescription(); echo "MIME type: ", $file->getMimeType(); } catch (Exception $e) { echo "An error occurred: ", $e->getMessage(); } ?>
All runs fine (no errors anyway) until the $service->files->get( GDRIVE_FILE_KEY )
call which triggers the exception:
An error occurred: Error calling GET
https://www.googleapis.com/drive/v2/files
: (403) Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
What am I doing wrong? I've pulled my hair out (well, what was left).
PHP 5.4 or greater with the command-line interface (CLI) and JSON extension installed. The Composer dependency management tool. A Google Cloud Platform project with the API enabled. To create a project and enable an API, refer to Create a project and enable the API.
To access the data stored in Google Sheets, you will need to create a service account and get a set of OAuth2 credentials from the Google API Console. Access the Google APIs Console while logged into your Google account. Create a new project and give it a name. Click on ENABLE APIS AND SERVICES .
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");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With