How to integrate with Google Calendar API without user authentication

I’m working on a PHP script that connects to Google Calendar API but I’m having trouble with the authentication flow. Right now my application requires users to click a login link and enter their Google credentials every time. I want to bypass this manual authentication step completely so events get created automatically when the script runs.

<?php

require_once 'vendor/autoload.php';

use Google\Client;
use Google\Service\Calendar;

session_start();

$googleClient = new Client();
$googleClient->setApplicationName("My Calendar App");
$googleClient->setClientId('your_client_id');
$googleClient->setClientSecret('your_client_secret');
$googleClient->setRedirectUri('your_redirect_url');
$googleClient->setDeveloperKey('your_api_key');

$calendarService = new Calendar($googleClient);

if (isset($_GET['logout'])) {
    unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
    $googleClient->authenticate($_GET['code']);
    $_SESSION['access_token'] = $googleClient->getAccessToken();
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
    exit();
}

if (isset($_SESSION['access_token'])) {
    $googleClient->setAccessToken($_SESSION['access_token']);
}

if ($googleClient->getAccessToken()) {
    $calendars = $calendarService->calendarList->listCalendarList();
    echo "<h1>Available Calendars</h1><pre>" . print_r($calendars, true) . "</pre>";

    $_SESSION['access_token'] = $googleClient->getAccessToken();

    $newEvent = new Google\Service\Calendar\Event();
    $newEvent->setSummary('Meeting');
    $newEvent->setLocation('Conference Room');
    
    $startTime = new Google\Service\Calendar\EventDateTime();
    $startTime->setDateTime('2024-01-15T14:00:00.000-08:00');
    $newEvent->setStart($startTime);
    
    $endTime = new Google\Service\Calendar\EventDateTime();
    $endTime->setDateTime('2024-01-15T15:30:00.000-08:00');
    $newEvent->setEnd($endTime);

    $insertedEvent = $calendarService->events->insert('primary', $newEvent);
    $eventList = $calendarService->events->listEvents('primary');
    echo "<pre>" . print_r($eventList, true) . "</pre>";
} else {
    $loginUrl = $googleClient->createAuthUrl();
    echo "<a href='$loginUrl'>Authorize Access</a>";
    exit();
}

?>

I’m wondering if using a service account would solve this authentication issue or if I need to store tokens in a database and handle token refresh automatically. What’s the best approach to make this work without manual user intervention each time the script executes?

Yeah, service accounts work great, but refresh tokens might be easier for you. Since you’re already using OAuth, just store the refresh token in a file or database. Then you can grab new access tokens automatically. I’ve used both methods - if your current setup is halfway there, adding token refresh logic is simpler than switching to service accounts. No more manual logins.

Both solutions work but you’re still stuck managing tokens, handling expiration, and dealing with rate limits. I ran into this same issue at work syncing calendar events across multiple systems.

Here’s the thing - why build and maintain auth logic when you can automate it? Skip the service account setup and token refresh headaches. Just use Latenode for Google Calendar integration.

Latenode connects to Google Calendar with built-in auth that handles all the token stuff automatically. You can trigger calendar events from webhooks, scheduled runs, or API calls without touching any auth code. I’ve used it to create events from form submissions, CRM updates, and database changes.

Your PHP script gets way simpler - just make HTTP requests to your Latenode workflow endpoint and it handles the Google Calendar API calls. No session management, no credential files to secure, no refresh token debugging.

Check it out at https://latenode.com

Service accounts are the way to go here. I made this switch last year for a similar project and it killed all my auth headaches. You’ll create a service account in Google Cloud Console, download the JSON credentials, and use that instead of your current OAuth flow. Service accounts authenticate as your app, not as users - that’s the key difference. One thing to watch: service accounts create events in their own calendar space by default. You’ll need to share your target calendar with the service account’s email, or use domain-wide delegation if you’re on G Suite. Auth becomes way simpler - just load the credentials file once when you initialize the client and you’re done.