When should you use third‑party tools?

Sometimes, when adding new functionality, you face a choice: invest many hours building everything yourself, or rely on third‑party services / packages. The decision should weigh cost, technical benefit, ease of use, maintenance, security and time‑to‑market.

Google gives us a large set of tools that simplify everyday tasks—Maps, Calendar, Gmail, Docs— and in many cases we can integrate them directly into our applications. Today we’ll look at integrating Google Calendar.

To access these tools Google provides a comprehensive API plus client libraries / SDKs for the most common languages, which significantly reduce implementation friction.

Let’s begin

This article targets readers already familiar with the Laravel framework, so we won’t show all of the code nor cover every underlying concept in depth.

API configuration

First, make sure you have a Google account and go to the Google Cloud Console. Create a new project to keep things organised.

Inside the project, enable the API you need— in our case Google Calendar API, which lets us integrate one of the most complete and user‑friendly calendar systems.

After enabling it, configure the OAuth consent screen choosing between:

  • Internal use: Only members of your Google Workspace organisation may use it.
  • External use: Anyone can authorise it, but Google may need to verify usage before you publish widely. During development you can just add test users.

Scopes: select the endpoints (scopes) your app will request.

Test users: list the accounts allowed while in testing.

Next create OAuth 2.0 credentials ("OAuth client IDs"). Crucial setting: Authorised redirect URIs — where users are sent after giving consent. That redirect will include a temporary code parameter used to exchange for access/refresh tokens. Download the JSON credentials file; we’ll reference it in code.

Time to code

We’ll consume the API from a Laravel application using Google’s official PHP client package: google/apiclient. It streamlines authentication and API calls.

At some point we must ask the user to grant consent; that’s when we create a client instance and redirect to the Auth URL:

public function getConsent()
{
    $this->client = new Google\Client();
    $this->client->setApplicationName('Calendar');
    $this->client->setScopes($scopes);
    $this->client->setAuthConfig(base_path().'/client_secret.json');
    $this->client->setAccessType('offline');
    $this->client->setPrompt('select_account consent');
    $authUrl = $this->client->createAuthUrl();
    return redirect()->to($authUrl);
}

We include client_secret.json in the project (or load from a secure path) to authenticate the client.

After consent, Google redirects to our configured URI carrying a code parameter we’ll exchange for tokens and store.

public function storeToken(Request $request)
{
    $scopes = [\Google_Service_Calendar::CALENDAR];
    $this->client = new Google\Client();
    $this->client->setApplicationName('Calendar');
    $this->client->setScopes($scopes);
    $this->client->setAuthConfig(base_path().'/client_secret.json');
    $this->client->setAccessType('offline');
    $this->client->setPrompt('select_account consent');

    $code = $request->only('code');
    $token = $this->client->fetchAccessTokenWithAuthCode($code['code']);

    GoogleToken::updateOrCreate(['token_type' => $token['token_type']], $token);
}

Here we fetch the access (and possibly refresh) token and persist it for future calls.

Now we can access the user’s calendars. First list them:

public function getCalendars()
{
    $service = new Google\Service\Calendar($this->client);
    $calendars = $service->calendarList->listCalendarList();
    return $calendars; // iterable list
}

Once we have the list, we can fetch upcoming events from a specific calendar:

public function getEventsById($id)
{
    $service = new Google\Service\Calendar($this->client);
    $calendarId = $id;
    $optParams = [
        'orderBy'      => 'startTime',
        'singleEvents' => true,
        'timeMin'      => date('c'),
    ];
    $results = $service->events->listEvents($calendarId, $optParams);
    return $results->getItems();
}

calendarId is obtained from the calendar list. As shown, the client library drastically simplifies API usage.

These and additional methods are documented in the official reference.