niisan/laravel-oauth-google-calendar

Using Google Calendar Api by using OAuth in Laravel.

v1.3.0 2021-06-01 05:26 UTC

This package is auto-updated.

Last update: 2024-04-29 04:34:14 UTC


README

This is a package of a series of processes to create an application linked to Google Calendar using OAuth authentication in Laravel.

Required

Laravel >= 5.8 PHP >= 7.4

Install

Install via composer.

composer require niisan/laravel-oauth-google-calendar

Then, bring the config to your config dir.

php artisan vendor:publish

and choose 'Niisan\Laravel\GoogleCalendar\OauthCalendarServiceProvider'.

You can use this package via DI container.

    private OauthCalendarService $oauthCalendarService;

    public function __construct(OauthCalendarService $oauthCalendarService)
    {
        $this->oauthCalendarService = $oauthCalendarService;
    }

or

    $service = app(OauthCalendarService::class);

Config

This package's config file is the google-calendar.php.

<?php
return [
    'scopes' => [
        'profile',
        'email',
        'https://www.googleapis.com/auth/calendar.events',
    ],
    'redirect' => env('GOOGLE_CALENDAR_OAUTH_REDIRECT_URL', 'https://example.com/auth/callback'),
    'client_id' => env('GOOGLE_CALENDAR_CLIENT_ID', ''),
    'client_secret' => env('GOOGLE_CALENDAR_CLIENT_SECRET', ''),
    'events' => [
        'token_refreshed' => ''
    ],
    'holiday_id' => env('GOOGLE_CALENDAR_HOLIDAY_ID', 'japanese__ja@holiday.calendar.google.com'),
];

client_id and client_secret are from Google OAuth user.

holiday_id is your county's calendar id offering holidays.

events.token_refreshed can define a event when token refreshed in this package.

For example,

'events' => [
    'token_refreshed' => TokenRefreshedEvent::class
]

And, Events/TokenRefreshedEvent.php

class TokenRefreshedEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($user, array $token)
    {
        //
    }

Usage

This package has following methods.

public function getEventList($user, $config): array;
public function createEvent($user, array $data): Google_Service_Calendar_Event;
public function deleteEvent($user, $event_id);

These methods take $user as an argument. $user is a object and must huve accessible propeties, access_token, refresh_token, expires.

getEventList()

This method get user's event. $config is a search condition.

$config = [
    'orderBy' => null,
    'search' => 'test',
    'timeMax' =>'2021-01-01',
    'timeMin' => '2020-12-01',
    'timeZone' => 'Asia/Tokyo',
    'updatedMin' => null
    'maxResults' => 150,
    'singleEvents' => true
];

All parameters are optional.

createEvent

This method create user's event. $data is a event content.

$data = [
    'summary' => 'abcd',
    'description' => 'efgh',
    'start' => '2021-02-21 12:00:00',
    'end' => '2021-02-21 13:00:00'
];

getFreeBusy

public function getFreeBusy($user, $config): array

This method give you user's busy time. The response type is following:

[
    [
        'start' => '2021-06-01T02:00:00Z'
        'end' => '2021-06-01T04:00:00Z'
    ],
    [
        'start' => '2021-06-07T02:00:00Z'
        'end' => '2021-06-07T04:00:00Z'
    ],
]

Note that https://www.googleapis.com/auth/calendar.readonly is the scope that the getFreeBusy method needs to be in.

updateEvent

this method update user's event. $data is same as createEvent's and put null for values that do not need to be updated.

getHolidays

This method offers you your country's holidays.