Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kashorako's avatar

Client is unauthorized to retrieve access tokens using this method (GOOGLE CALENDAR API - LARAVEL)

Hello guys i'm having problems trying to use the Google Calendar API, What i want to achieve is, when i create a reminder from my system (CRM in Laravel), give the user the option to add a reminder to they Google Calendar. I have a Gmail account in which i enabled the Google Calendar API, i also created the service account, the keys, generated the JSON file, shared the calendar, etc.

The next code works just fine:

putenv('GOOGLE_APPLICATION_CREDENTIALS='.storage_path('app/google-calendar/testcalendar-9a1016-8e7e06d45a54.json').'');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();//BUSCARÁ GOOGLE_APLICATION_CREDENTIALS EN LA CONFIGURACION (.ENV)
$client->addScope(Google_Service_Calendar::CALENDAR);

###Instanciamos el servicio en base a $client
$service = new Google_Service_Calendar($client);
##SIEMPRE OBTENDREMOS EL ID DE CALENDARIO
$calendarId = 'info.soft***[email protected]';//EL ID DEL CALENDARIO CUANDO COMPARTIMOS Y CONFIGURAMOS UN CALENDARIO DESDE EL PORTAL DE GOOGLE CALENDAR

####crear
##VARIABLES
$title = "Evento 21 de agosto, 22xx";
$description = "Creado desde Laravel para el 21 de agosto";
$startDateTime = Carbon\Carbon::now()->addHours(10);
$endDateTime = Carbon\Carbon::now()->addHours(11);
##FIN_VARIABLES
$event = new Google_Service_Calendar_Event([
    'summary' => $title,
    'description' => $description,
    'start' => ['dateTime' => $startDateTime],
    'end' => ['dateTime' => $endDateTime],
    'reminders' => ['useDefault' => true],
]);
$result = $service->events->insert($calendarId, $event);
dump($result);

With the previous code i am able to add an event to my calendar, the problem occurs when i try to add an attendee to the event:

putenv('GOOGLE_APPLICATION_CREDENTIALS='.storage_path('app/google-calendar/testcalendar-287016-9a1e06d45a54.json').'');
$client = new Google_Client();
$client->setAuthConfig(storage_path('app/google-calendar/testcalendar-287016-9a1e06d45a54.json'));//funciona igual que el env
$client->addScope(Google_Service_Calendar::CALENDAR);
$service = new Google_Service_Calendar($client);
$calendarId = 'info.softw***[email protected]';//EL ID DEL CALENDARIO CUANDO COMPARTIMOS Y CONFIGURAMOS UN CALENDARIO DESDE EL PORTAL DE GOOGLE CALENDAR
##IMPERSONATION
$client->setSubject('dra***[email protected]');
####crear
$event = new Google_Service_Calendar_Event(array(
    'summary' => 'Probando con Attendee 1513',
    'location' => 'Villa Salome, Calle Alamos #95',
    'description' => 'Prueba de evento agregando attendees.',
    'start' => array(
        'dateTime' => '2020-08-23T09:00:00-07:00',
        'timeZone' => 'America/La_Paz',
    ),
    'end' => array(
        'dateTime' => '2020-08-23T17:00:00-07:00',
        'timeZone' => 'America/La_Paz',
    ),
    'attendees' => array(
        array('email' => 'dra***[email protected]'),
    ),
    'reminders' => array(
        'useDefault' => FALSE,
        'overrides' => array(
            array('method' => 'email', 'minutes' => 24 * 60),
            array('method' => 'popup', 'minutes' => 10),
        ),
    ),
));
$event = $service->events->insert($calendarId, $event);

With the previous code i receive the error:

Google_Service_Exception
{ "error": "unauthorized_client", "error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested." } 

I just want to add an event to their Gmail accounts when they pick that. I don't have a G Suite account, i've read a lot of threads and i also activated the "Domain-Wide Delegation of Authority" in the Service Account settings in Google Console. I also tried using this package: https://packagist.org/packages/spatie/laravel-google-calendar But it does not work either. Thanks for your help!

0 likes
5 replies
Kashorako's avatar

Yes i can list and add events, but when i try to add and attendee i get that error, i've been reading and i found in stackoverflow that since march 2 all service accounts must impersonate the user, that's why i used "$client->setSubject('dra***[email protected]');" but it does not work either, i also read in other posts that, now the service accounts must haave a G Suite to send or add events in other's calendar. I tried creating three service accounts and the problem is the same, i can't list and create events, but it crashes when i use attendee

Please or to participate in this conversation.