asimlqt / php-google-spreadsheet-client

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

500 Error for $spreadsheetService->getSpreadsheets(); #133

Closed mattshoaf closed 8 years ago

mattshoaf commented 8 years ago

I've created an API account and tested it with the Drive API, am able to retrieve the file info as expected when I use:

$service = new Google_Service_Drive($client);
$file = $service->files->get($fileId);

However, haven't gotten around the 500 Service unavailable error when I do this:

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

Here's how I set it's bootstrapped, if you can see what I'm missing, it would be much appreciated:

require_once 'sites/all/libraries/vendor/autoload.php';

require_once 'sites/all/libraries/google-api-php-client-1-master/src/Google/autoload.php';

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

$client_id = '[client-id]'; 
$service_account_name = '[service-email]@wise-pentameter-121815.iam.gserviceaccount.com'; $key_file_location = 'sites/all/libraries/[private-key].p12'; 
$private_key = file_get_contents($key_file_location);
$scopes = [
    'https://www.googleapis.com/auth/sqlservice.admin',
    'https://spreadsheets.google.com/feeds',
    'https://docs.google.com/feeds',
        'https://www.googleapis.com/auth/drive'
];

$credentials = new Google_Auth_AssertionCredentials(
  $service_account_name,
  $scopes,
  $private_key
);

$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion();
}
$client->setApplicationName("Write_Forms_to_Drive");

$accessToken = $client->getAccessToken();

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

$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();

//this blows up!
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();

//this works fine
$service = new Google_Service_Drive($client);
$file = $service->files->get([fileId]);
mattshoaf commented 8 years ago

Never mind, finally figured out I hadn't decoded the AccessToken json.

If anybody else runs into this, replace $accessToken = $client->getAccessToken(); with

$service_token = json_decode($client->getAccessToken());

$accessToken = $service_token->access_token;
samoum23 commented 8 years ago

hello , i just follow u to can retrieve data from spreadsheet in drive but i got this message

You need permission 500 Internal Server Error - UnauthorizedException

please any help