2

I'm using the php-google-spreadsheet-client to get the list of google sheets. Here is the code I am using:

<?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(
            '[email protected]',
            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);
    }

But the getSpreadsheets() function is returning an empty array. How can I get the list of my google sheets?

2
  • What line is the error on? Commented Feb 4, 2015 at 3:46
  • @SandyGood There are no errors. But I managed to fix it. I didn't know that I had to share the sheets with the client email inorder to get it. Just waiting to see if some will post an answer. Commented Feb 4, 2015 at 11:08

1 Answer 1

2

I know what the answer is. You need to share the sheets with the client email in order to get it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.