asimlqt / php-google-spreadsheet-client

A PHP library for accessing and manipulating Google Spreadsheets
Other
544 stars 152 forks source link

getSpreadsheets() returns empty list #22

Closed Schepp closed 10 years ago

Schepp commented 10 years ago

Hey Asim, thank you for putting that much effort into this thing!

Still, I've got to report a problem. I think I got everything set up correctly, but when I want to retrieve the list of spreadsheets located under my account all I get is an empty list. On the other hand, when I'm logged into Google and open https://spreadsheets.google.com/feeds/spreadsheets/private/full in the browser I do see an extensive list of documents. Any idea why that is? I published the spreadsheet and I also set it to publicly visible, but no luck...

Here is my code:

<?php

// Config
require_once __DIR__ . '/../config.php';

// Autoload Composer packages
require __DIR__ . '/../vendor/autoload.php';

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

$client = new Google_Client();
$client->setApplicationName('My Webservice');
$client->setClientId(GOOGLE_CLIENTID);

$cred = new Google_Auth_AssertionCredentials(
    GOOGLE_CLIENTEMAIL,
    array('https://spreadsheets.google.com/feeds'),
    file_get_contents(GOOGLE_CLIENTKEYPATH)
);

$client->setAssertionCredentials($cred);

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

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

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

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

Thank you in advance for your help!

asimlqt commented 10 years ago

I've created a new project to help get started with OAuth.

https://github.com/asimlqt/php-google-oauth

ZurabWSG commented 9 years ago

the php-google-oauth is for web authentication involving third-party user interaction. How to handle this with service accounts? My spreadsheets won't show up.

davidmaneuver commented 9 years ago

I have been struggling with this for a few hours.

You should mimic your own Google Apps account and setup your credentials like this:

$cred = new Google_Auth_AssertionCredentials(
    GOOGLE_CLIENTEMAIL,
    array('https://spreadsheets.google.com/feeds'),
    file_get_contents(GOOGLE_CLIENTKEYPATH),
    'notasecret',
    'http://oauth.net/grant_type/jwt/1.0/bearer',
    'user@example.org'
);

Follow this to set the right permissions for your account: https://developers.google.com/drive/web/delegation

ZurabWSG commented 9 years ago

This is only for google enterprise accounts. Free google accounts don't have their own apps domain and therefore can't delegate domain-wide authority to a service account. Not an option for me.

alfiebesin commented 8 years ago

I know this is a bit old but might help someone who faces the same issue. You just need to share the spreadsheet to your service account email so it will be able to lists it. Thanks