John Davidson

Google api v3 calandar quickadd php

0 comments
Message:


I'm trying to create events in my google calendar with the google quickadd API.
Here is my code:


require_once 'googleapi/vendor/autoload.php';

$client = new Google\Client();
$client->setApplicationName("Google Calendar API");
$client->setDeveloperKey("AIzaSyDdR3w2J59ZVVv_s7BopzNuteu1Bzo");
$client->setAuthConfig('googleapi/code_secret_client_62725432848-um1gbjoo7iqi1upvmv4niosddfte43.apps.googleusercontent.com.json');
$client->addScope("https://www.googleapis.com/auth/calendar.events");

$service = new Google_Service_Calendar($client);
// echo "<pre>";
// print_r($service);
// echo "</pre>";
$createdEvent = $service->events->quickAdd(
'[email protected]',
'Appointment at Somewhere on June 3rd 10am-10:25am'
);

echo $createdEvent->getId();

I keep getting the error below. Can someone please tell me why?


Fatal error: Uncaught Google\Service\Exception: { "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Login Required.", "domain": "global", "reason": "required", "location": "Authorization", "locationType": "header" } ], "status": "UNAUTHENTICATED" } } in C:\xampp\htdocs\google quick add\googleapi\src\Http\REST.php:128 Stack trace: #0 C:\xampp\htdocs\google quick add\googleapi\src\Http\REST.php(103): Google\Http\REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 [internal function]: Google\Http\REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 C:\xampp\htdocs\google quick add\googleapi\src\Task\Runner.php(18 in C:\xampp\htdocs\google quick add\googleapi\src\Http\REST.php on line 128



My Answer:
>setAuthConfig('credentials.json');
$client->addScope(Google_Service_Calendar::CALENDAR);
$service = new Google_Service_Calendar($client);

$event = new Google_Service_Calendar_Event(array(
'summary' => 'Test Event',
'description' => 'This is a test event',
'start' => array(
'dateTime' => '2022-01-01T10:00:00',
'timeZone' => 'America/Los_Angeles',
),
'end' => array(
'dateTime' => '2022-01-01T12:00:00',
'timeZone' => 'America/Los_Angeles',
),
));

$calendarId = 'primary';
$event = $service->events->quickAdd($calendarId, 'Test Event on 2022-01-01 from 10am to 12pm');

echo 'Event created: ' . $event->htmlLink;

Rate this post

4 of 5 based on 6801 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us