niisan/laravel-oauth-google-calendar

使用OAuth在Laravel中通过Google Calendar Api。

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

This package is auto-updated.

Last update: 2024-08-29 05:49:55 UTC


README

这是一个通过Laravel中OAuth认证创建与Google Calendar链接的应用程序的一系列过程。

需求

Laravel >= 5.8 PHP >= 7.4

安装

通过composer安装。

composer require niisan/laravel-oauth-google-calendar

然后,将配置文件放到您的配置目录中。

php artisan vendor:publish

并选择 'Niisan\Laravel\GoogleCalendar\OauthCalendarServiceProvider'。

您可以通过DI容器使用此包。

    private OauthCalendarService $oauthCalendarService;

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

    $service = app(OauthCalendarService::class);

配置

此包的配置文件是 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_idclient_secret 来自Google OAuth用户。

holiday_id 是提供假期的您所在县的日历ID。

events.token_refreshed 可以定义在此包中令牌刷新时的事件。

例如,

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

并且, Events/TokenRefreshedEvent.php

class TokenRefreshedEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

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

用法

此包有以下方法。

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

这些方法将 $user 作为参数。 $user 是一个对象,必须要有可访问的属性,access_tokenrefresh_tokenexpires

getEventList()

此方法获取用户的事件。 $config 是搜索条件。

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

所有参数都是可选的。

createEvent

此方法创建用户的事件。 $data 是事件内容。

$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

此方法为您提供用户的忙碌时间。响应类型如下

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

注意,https://www.googleapis.com/auth/calendar.readonlygetFreeBusy 方法所需的范围。

updateEvent

此方法更新用户的事件。 $data 与 createEvent 相同,对于不需要更新的值请设置为null。

getHolidays

此方法为您提供您所在国的节假日。