Trying to access GoogleSheet by using google-api-php-client but failed

I am trying to access Google Sheet using php through google-api-php-client library but getting AuthError exception.
index.html
<form action="post2sheet.php" method="post">
First name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
Phone: <input type="text" name="phone"><br>
Position: <input type="text" name="position"><br>
<input type="submit" value="Submit">
</form>
post2sheet.php
<?php
require_once '/vendor/autoload.php'; // google-api-php-client path
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Project');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
//PATH TO JSON FILE DOWNLOADED FROM GOOGLE CONSOLE FROM STEP 7
$client->setAuthConfig('demo-3c0c2-1a1a604c5***.json');
$client->setAccessType('offline');
return $client;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Sheets($client);
$spreadsheetId = '1MFNNG4yVDAsz9h1nAEGa0ZftixB2fF2lwvWhvf94***'; // spreadsheet Id
$range = 'Sheet1'; // Sheet name
$valueRange= new Google_Service_Sheets_ValueRange();
$valueRange->setValues(["values" => ["a", "b"]]); // values for each cell
$valueRange->setValues(["values" => [
$_POST["name"]
, $_POST["email"]
, $_POST["position"]
, $_POST["phone"]
, date("F j, Y, g:i a", time())
]]);
$conf = ["valueInputOption" => "RAW"];
$response = $service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $conf);
but geting an exception
Fatal error: Uncaught Google_Service_Exception: { "error": { "code": 403, "message": "The caller does not have permission", "errors": [ { "message": "The caller does not have permission", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } } in C:\xampp\htdocs\Demo\src\Google\Http\REST.php:123 Stack trace: #0 C:\xampp\htdocs\Demo\src\Google\Http\REST.php(98): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 C:\xampp\htdocs\Demo\src\Google\Task\Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 C:\xampp\htdocs\Demo\src\Google\Http\REST.php(61): Google_Task_Runner->run() #3 C:\xampp\htdocs\Demo -PH in C:\xampp\htdocs\Demo\src\Google\Http\REST.php on line 123
Is there any other way to implement this!!
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xmlhttprequest error flutter
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.