• 3
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How do I read a Google Drive Spreadsheet in PHP?

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 :

  1. Been to the Google APIs Console and :
    1. Enabled "Drive API" and "Drive SDK" under 'Services';
    2. Created an OAuth 2.0 client ID under 'API Access'. Under "Client ID for web applications", the console gave me "Client ID", "Email address", "Client secret", "Redirect URIs" and "JavaScript origins";
  2. Downloaded the "Google API PHP Client Library";
  3. Opened the Google Drive document (spreadsheet) and clicked on "Share" to get the document's 'key';
  4. Set up the following code :
<?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).

There is also a much easier, but less clean solution, if you don't want to bother with the API or Google Authentication.

  1. Go to your Spreadsheet in Google Drive.
  2. Go to files -> Publish on the Web
  3. Select the right worksheet and make sure to enable the checkbox always update.
  4. Still in the same box you can get a .csv Link to your spreadsheet.

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");
  • 69
Reply Report

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.

  • 7
Reply Report
    • If the user runs this script, he/she has already gone through local Apache authentication. Can I re_use the same GDRIVE_CLIENT_ID and GDRIVE_CLIENT_SECRET? When the script runs, I want to unconditionaly read the Google Drive Spreadsheet. I don't want the visitor to have to re-authenticate.
      • 2
    • But, the Google file is MY file. I maintain it. I just want MY PHP script to read it. It is as if I maintain an included file on Google Drive. I want my script to unconditionally read this document with my Google Drive access codes. The visitor does not need to log in. He/she still can't modify my document; they can only see it.

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

  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions