I embarked on this project thinking it would be simple. Many hours later I\'m realizing the Google API is a bit of a labyrinth with multiple APIs and libraries. I really nee
This answer is meant to be an add-on to jrgd`s answer, and I post it here, as an answer, because it contains code.
The connection problems themselves hung me up. Here is what I had to do to resolve that: Trying to Get Google accessToken . Also, the spreadsheet has to be shared with the Google-generated e-mail that you find in your code.
Also, jrgd, to answer your question about that Request object: it doesn't exist with the Google Spreadsheet API; /* I couldn't find it when I used Composer to download the library */
Instead, I ended up doing what GingerDog did:
$serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($accessToken);
Google\Spreadsheet\ServiceRequestFactory::setInstance($serviceRequest);
There is also a significant probability of the code throwing an Exception when using getSpreadsheets(), due to an HTTP error code being returned that is higher than 300 . The SpreadsheetService class has that method (here is its code:
/**
* Fetches a list of spreadhsheet spreadsheets from google drive.
*
* @return \Google\Spreadsheet\SpreadsheetFeed
*/
public function getSpreadsheets()
{
return new SpreadsheetFeed(
ServiceRequestFactory::getInstance()->get('feeds/spreadsheets/private/full')
);
}
Notice that there is another class that is "doing the dirty work here": the DefaultServiceRequest class. Here is the get() that is being used:
/**
* Perform a get request
*
* @param string $url
*
* @return string
*/
public function get($url)
{
$ch = $this->initRequest($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
return $this->execute($ch);
}
/**
* Executes the api request.
*
* @return string the xml response
*
* @throws \Google\Spreadsheet\Exception If the was a problem with the request.
* Will throw an exception if the response
* code is 300 or greater
*
* @throws \Google\Spreadsheet\UnauthorizedException
*/
protected function execute($ch)
{
$ret = curl_exec($ch);
$info = curl_getinfo($ch);
$httpCode = (int)$info['http_code'];
if($httpCode > 299) {
if($httpCode === 401) {
throw new UnauthorizedException('Access token is invalid', 401);
} else {
throw new Exception('Error in Google Request', $info['http_code']);
}
}
return $ret;
}
Notice that the function, from its innermost helper, has a chance to give back an http_code that will cause the code to throw an exception. Not good for business.
The Solution
The way I remedied that is to change the following line of code:
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
to this while-loop:
/* my way of "making" it work; // I just getSpreadsheets() until there stops being an exception thrown */
$googleException = new Exception();
while ($googleException != null)
{
try
{
$spreadsheetFeed = $spreadsheetService->getSpreadsheets(); # This line randomly throws exception, for some reason.
$googleException = null;
}
catch (Exception $e)
{
$googleException = $e;
}
}
//var_dump($spreadsheetFeed->getArrayCopy()); // test line