'I need to link Google Sheet with my Laravel
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
Solution 1:[1]
Same issue for me :
ErrorException array_key_exists() expects parameter 2 to be array, null given C:\laragon\www\wonood\vendor\google\auth\src\Middleware\AuthTokenMiddleware.php:124
My controller ?
<?php
namespace App\Http\Controllers;
use Revolution\Google\Sheets\Facades\Sheets;
class MagicViewController extends Controller
{
public function google()
{
// Add new sheet to the configured google spreadsheet
Sheets::spreadsheet('19eKwKwxxxKsd6cSM')->addSheet('sheet 3');
$rows = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
];
// Append multiple rows at once
Sheets::sheet('sheetTitle')->append($rows);
return view('magic_view');
}
}
My .env ?
GOOGLE_APPLICATION_NAME=MyApp
SPREADSHEET_ID=19eKwKwM0wGhXXXXXXXXXd6cSM
GOOGLE_CLIENT_ID=2410oksuct.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=XXXXXXXXX3IFqwVsd7kYFXXXbWd
GOOGLE_REDIRECT=
GOOGLE_DEVELOPER_KEY=
GOOGLE_SERVICE_ENABLED=true
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=./storage/credentials.json
GOOGLE_APPLICATION_CREDENTIALS=./storage/credentials.json
My google config in /config/google.php ?
<?php
return [
/*
|----------------------------------------------------------------------------
| Google application name
|----------------------------------------------------------------------------
*/
'application_name' => env('GOOGLE_APPLICATION_NAME', 'MyApp'),
/*
|----------------------------------------------------------------------------
| Google OAuth 2.0 access
|----------------------------------------------------------------------------
|
| Keys for OAuth 2.0 access, see the API console at
| https://developers.google.com/console
|
*/
'client_id' => env('GOOGLE_CLIENT_ID', '2410oksuct.apps.googleusercontent.com'),
'client_secret' => env('GOOGLE_CLIENT_SECRET', 'XXXXXXXXX3IFqwVsd7kYFXXXbWd'),
'redirect_uri' => env('GOOGLE_REDIRECT', ''),
'scopes' => [],
'access_type' => 'online',
'approval_prompt' => 'auto',
/*
|----------------------------------------------------------------------------
| Google developer key
|----------------------------------------------------------------------------
|
| Simple API access key, also from the API console. Ensure you get
| a Server key, and not a Browser key.
|
*/
'developer_key' => env('GOOGLE_DEVELOPER_KEY', ''),
/*
|----------------------------------------------------------------------------
| Google service account
|----------------------------------------------------------------------------
|
| Set the credentials JSON's location to use assert credentials, otherwise
| app engine or compute engine will be used.
|
*/
'service' => [
/*
| Enable service account auth or not.
*/
'enable' => env('GOOGLE_SERVICE_ENABLED', false),
/*
* Path to service account json file. You can also pass the credentials as an array
* instead of a file path.
*/
'file' => env('GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION', './storage/credentials.json'),
],
/*
|----------------------------------------------------------------------------
| Additional config for the Google Client
|----------------------------------------------------------------------------
|
| Set any additional config variables supported by the Google Client
| Details can be found here:
| https://github.com/google/google-api-php-client/blob/master/src/Google/Client.php
|
| NOTE: If client id is specified here, it will get over written by the one above.
|
*/
'config' => [
'spreadsheet_id'=> '19eKwKwM0wGhXXXXXXXXXd6cSM',
// 'google_application_credentials'=> './storage/credentials.json',
],
];
Note :
- I share spreadsheet with my google service account mail : [email protected]
- Drive and Sheet API are actived
- OAuth is actived
I work on development with : laragon - Php 7.2.19 - Laravel Framework 7.28.4
Solution 2:[2]
Solution :
2 - enable the google sheets api and create credentials :
After installing and configure your API :
3 - Create a new a Controller :
<?php
namespace App\Services;
use Google_Client;
use Google_Service_Sheets;
use Google_Service_Drive ;
use Google_Service_Sheets_ValueRange;
use App\Models\Api\SheetAuth as ApiSheetAuth;
use App\Models\Colis;
use Google\Service\ServiceControl\Auth;
use App\Models\TemplateFields;
use App\Http\Controllers\callcenter\DispatchCallsController as CallCenter;
class GoogleSheet
{
private $spreadSheetId;
private $client;
public $googleSheetService;
public function __construct()
{
// $this->spreadSheetId = config('datastudio.google_sheet_id');
$this->client = new Google_Client();
$this->client->setAuthConfig(storage_path('credentials.json'));
$this->client->addScope("https://www.googleapis.com/auth/spreadsheets");
$this->googleSheetService = new Google_Service_Sheets($this->client);
}
Solution 3:[3]
I got the same issue and I solved it by the following solution
in config/google in the set scope as
scope=>[\Google_Service_Sheets::DRIVE, \Google_Service_Sheets::SPREADSHEETS]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Antwan |
Solution 2 | MADI |
Solution 3 | Juliette Wu |