asimlqt / php-google-spreadsheet-client

A PHP library for accessing and manipulating Google Spreadsheets
Other
543 stars 154 forks source link

no spreadsheets when calling getSpreadsheets() #84

Closed mikkoc closed 9 years ago

mikkoc commented 9 years ago

Since ClientLogin was deprecated a couple weeks ago I started looking into using this php API to manage my spreadsheets. I created a new API project from the Google console, got all the secrets, keys, p12 file, etc. Below is the code I'm using.

The last line fails because the $spreadsheet object is NULL.

<?php

require 'vendor/autoload.php';

const G_CLIENT_ID = '.....apps.googleusercontent.com';
const G_CLIENT_EMAIL = '....@developer.gserviceaccount.com';
const G_CLIENT_KEY_PATH = '/home/mikkoc/12345-privatekey.p12';
const G_CLIENT_KEY_PW = 'notasecret';

$client = new Google_Client();
$client->setApplicationName('whmcs-spreadsheet');
$client->setClientId(G_CLIENT_ID);

$key = file_get_contents(G_CLIENT_KEY_PATH);
$cred = new Google_Auth_AssertionCredentials(
    G_CLIENT_EMAIL,
    array('https://spreadsheets.google.com/feeds'),
    $key,
    G_CLIENT_KEY_PW
);

$client->setAssertionCredentials($cred);

if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}

$service_token = json_decode($client->getAccessToken());
print_r($service_token);
$accessToken = $service_token->access_token;

use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;

$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);

$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();

var_dump($spreadsheetFeed);

$spreadsheet = $spreadsheetFeed->getByTitle('servers_GLOBAL');

var_dump($spreadsheet); //this prints NULL

$worksheetFeed = $spreadsheet->getWorksheets();

Here's the XML feed return by $spreadsheetService->getSpreadsheets():

object(Google\Spreadsheet\SpreadsheetFeed)#10 (2) {
  ["xml":protected]=>
  object(SimpleXMLElement)#11 (5) {
    ["id"]=>
    string(63) "https://spreadsheets.google.com/feeds/spreadsheets/private/full"
    ["updated"]=>
    string(24) "2015-06-09T18:23:56.148Z"
    ["category"]=>
    object(SimpleXMLElement)#13 (1) {
      ["@attributes"]=>
      array(2) {
        ["scheme"]=>
        string(43) "http://schemas.google.com/spreadsheets/2006"
        ["term"]=>
        string(55) "http://schemas.google.com/spreadsheets/2006#spreadsheet"
      }
    }
    ["title"]=>
    string(100) "Available Spreadsheets - 123456789abcdef@developer.gserviceaccount.com"
    ["link"]=>
    array(3) {
      [0]=>
      object(SimpleXMLElement)#12 (1) {
        ["@attributes"]=>
        array(3) {
          ["rel"]=>
          string(9) "alternate"
          ["type"]=>
          string(9) "text/html"
          ["href"]=>
          string(22) "http://docs.google.com"
        }
      }
      [1]=>
      object(SimpleXMLElement)#14 (1) {
        ["@attributes"]=>
        array(3) {
          ["rel"]=>
          string(37) "http://schemas.google.com/g/2005#feed"
          ["type"]=>
          string(20) "application/atom+xml"
          ["href"]=>
          string(63) "https://spreadsheets.google.com/feeds/spreadsheets/private/full"
        }
      }
      [2]=>
      object(SimpleXMLElement)#15 (1) {
        ["@attributes"]=>
        array(3) {
          ["rel"]=>
          string(4) "self"
          ["type"]=>
          string(20) "application/atom+xml"
          ["href"]=>
          string(63) "https://spreadsheets.google.com/feeds/spreadsheets/private/full"
        }
      }
    }
  }
  ["storage":"ArrayIterator":private]=>
  array(0) {
  }
}

What am I missing? I have two spreadsheets, but none of them is shown in the feed...

mikkoc commented 9 years ago

Nevermind, I found out I had to share the spreadsheets with the 123456789abcdef@developer.gserviceaccount.com email created via the developer console. Closing.