i want to link my laravel app with Google sheet i use this package :
Google Sheets API v4 for Laravel
my function :
public function doPostData(){
$append = [
'name',
'[email protected]',
'joe',
];
Sheets::spreadsheet('1A5NSRIVN_uSBI2JcoJPNc7N0rvHL-dmIrg9_jgESscA')
->sheet('YouCan')
->append([$append]);
}
i get this error :
array_key_exists() expects parameter 2 to be array, null given
private function fetchToken()
{
$auth_tokens = $this->fetcher->fetchAuthToken($this->httpHandler);
if (array_key_exists('access_token', $auth_tokens)) {
// notify the callback if applicable
if ($this->tokenCallback) {
call_user_func(
$this->tokenCallback,
$this->fetcher->getCacheKey(),
$auth_tokens['access_token']
);
}
return $auth_tokens['access_token'];
my env file :
GOOGLE_APPLICATION_NAME=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT=
GOOGLE_DEVELOPER_KEY=
GOOGLE_SERVICE_ENABLED=true
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=../storage/credentials.json
Note : i create the Service Accounts + OAuth Id and i shared the file
My Answer:
protected function doPostData()
{
$client = new \Google_Client();
$client->setApplicationName('Your Application Name');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAuthConfig('path/to/your/credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$service = new \Google_Service_Sheets($client);
$spreadsheetId = 'your_spreadsheet_id';
$range = 'Sheet1!A1:B1';
$values = [
["John Doe", "[email protected]"]
];
$body = new \Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params = [
'valueInputOption' => 'RAW'
];
$result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params);
if ($result->getUpdates()->getUpdatedCells() > 0) {
return "Data successfully appended to Google Sheet";
} else {
return "Failed to append data to Google Sheet";
}
}
Rate this post
3 of 5 based on 2939 votesComments