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 GoogleClient(); $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( 'myemail@gmail.com', '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 GoogleServiceException: { “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:xampphtdocsgoogle quick addgoogleapisrcHttpREST.php:128 Stack trace: #0 C:xampphtdocsgoogle quick addgoogleapisrcHttpREST.php(103): GoogleHttpREST::decodeHttpResponse(Object(GuzzleHttpPsr7Response), Object(GuzzleHttpPsr7Request), ‘Google_Service_…’) #1 [internal function]: GoogleHttpREST::doExecute(Object(GuzzleHttpClient), Object(GuzzleHttpPsr7Request), ‘Google_Service_…’) #2 C:xampphtdocsgoogle quick addgoogleapisrcTaskRunner.php(18 in C:xampphtdocsgoogle quick addgoogleapisrcHttpREST.php on line 128
Advertisement
Answer
According to error messages you weren’t authorized.
these you can read about 401 HTTP code
And these you can see the example, right way to authorize your app