php-google-spreadsheet-client getSpreadsheets返回空数组

时间:2015-02-03 21:30:35

标签: php google-spreadsheet-api

我正在使用php-google-spreadsheet-client获取Google工作表的列表。这是我正在使用的代码:

<?php

    require 'vendor/autoload.php';
    require_once 'google-api-php-client/autoload.php';

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

    function getToken()
    {
        $client = new Google_Client();
        $client->setApplicationName('project');
        $client->setClientId('client_id');

        $key = file_get_contents('key.p12');
        $cred = new Google_Auth_AssertionCredentials(
            'id@developer.gserviceaccount.com',
            array('https://spreadsheets.google.com/feeds'),
            $key
        );

        $client->setAssertionCredentials($cred);

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

        $service_token = json_decode($client->getAccessToken());
        return $service_token->access_token;
    }

    $accessToken = getToken();
    $serviceRequest = new DefaultServiceRequest($accessToken);
    ServiceRequestFactory::setInstance($serviceRequest);
    $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
    $spreadsheetFeed = $spreadsheetService->getSpreadsheets();
    print_r(count($spreadsheetFeed));
    foreach($spreadsheetFeed as $spreadsheet){
        print_r($spreadsheet);
    }

但是getSpreadsheets()函数返回一个空数组。 如何获取我的Google表格列表?

1 个答案:

答案 0 :(得分:2)

我知道答案是什么。您需要与客户电子邮件共享工作表才能获得它。