Dev Dev - 1 year ago 75
PHP Question

unable to access google spreedsheets values using php

I'm installed
google api for OAuth2 php lib; here:
google spreadsheet api php lib; here:

created the credentials on the API Console using service account

Here is the php script I used in combination with the above:

require_once __DIR__.'/vendor/google/apiclient/src/Google/Auth/AssertionCredentials.php';

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

const G_CLIENT_ID = 'my_client_id';
const G_CLIENT_EMAIL = 'email address';
const G_CLIENT_KEY_PATH = 'key.p12';
const G_CLIENT_KEY_PW = 'notasecret';

$obj_client_auth = new Google_Client ();
$obj_client_auth -> setApplicationName ('newproject');
$obj_client_auth -> setClientId (G_CLIENT_ID);
$obj_client_auth -> setAssertionCredentials (new Google_Auth_AssertionCredentials (
file_get_contents (G_CLIENT_KEY_PATH),
$obj_client_auth -> getAuth () -> refreshTokenWithAssertion ();
$obj_token = json_decode ($obj_client_auth -> getAccessToken ());
$accessToken = $obj_token->access_token;
$serviceRequest = new DefaultServiceRequest($accessToken);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
$spreadsheet = $spreadsheetFeed->getByTitle('NewSpreadSheet');
$worksheetFeed = $spreadsheet->getWorksheets();
$worksheet = $worksheetFeed->getByTitle('sssNew Worksheet');
$listFeed = $worksheet->getListFeed();
$row = array('name'=>'John', 'age'=>25);

I receive the following error:
Call to a member function getWorksheets() on a non-object...
I'm not getting how correct it...
Any help n suggestions

Dev Dev
Answer Source

I got it... the issue is about i didn't share my spreadsheet... when i shared my spreadsheet with client email then i'm able to insert records into the desired sheets... now i'm able to access every sheet,insert,delete everything...

So, when ever anyone got this problem just share your sheet with your client email.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download