I have a Laravel project which I want to use the Google Sheets API on. I've created and double checked that all of the credentials generated on the Google API Console (API Keys, OAuth 2.0 Client IDs and Service Accounts) matched the config of the .env file and the config/google.php file.
config/google.php file (please note that the values inside of ' ' are entered correctly (this is just to ensure that the layout of the file is correct):
<?php
return [
/*
|----------------------------------------------------------------------------
| Google application name
|----------------------------------------------------------------------------
*/
'application_name' => env('GOOGLE_APPLICATION_NAME', ''),
/*
|----------------------------------------------------------------------------
| 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', ''),
'client_secret' => env('GOOGLE_CLIENT_SECRET', ''),
'redirect_uri' => env('GOOGLE_REDIRECT', 'http://127.0.0.1:8000'),
'scopes' => [\Google_Service_Sheets::DRIVE, \Google_Service_Sheets::SPREADSHEETS], // previously only []
'access_type' => 'online',
'approval_prompt' => 'auto',
'prompt' => 'consent',
/*
|----------------------------------------------------------------------------
| 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', true),
/*
| Path to service account json file
*/
'file' => storage_path('app/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' => [],
];
My controller function:
public function __invoke(Request $request)
{
$sheets = Sheets::spreadsheet('sheets.THE SHEET ID (cannot disclose)')
->sheetById('sheets.0')
->all();
$posts = array();
foreach ($sheets AS $data) {
$posts[] = array(
'name' => $data[0],
'message' => $data[1],
'created_at' => $data[2],
);
}
return view('apitest')->with(compact('posts'));
}
My blade file:
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', '') }}</title>
<link href="{{ mix('css/app.css') }}" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Google Sheets for Laravel Demo</h1>
<div class="row">
<div class="col-sm">
<form action="{{ route('post.store') }}" method="post">
@csrf
<div class="form-group">
<label for="name">Name</label>
<input name="name" type="text" class="form-control" id="name" required>
</div>
<div class="form-group">
<label for="message">Message</label>
<input name="message" type="text" class="form-control" id="message" required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<div class="col-sm">
<div class="list-group mt-3">
@foreach($posts as $post)
<div class="list-group-item list-group-item-action">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{ data_get($post, 'name') }}</h5>
</div>
<p class="mb-1">
{{ data_get($post, 'message') }}
</p>
<small>
{{ data_get($post, 'created_at') }} {{ config('app.timezone') }}
</small>
</div>
@endforeach
</div>
</div>
</div>
</div>
<script src="{{ mix('js/app.js') }}"></script>
</body>
However, when trying to access the page it always gives the following error which doesn't quite make sense given that the API Key is correct:
