rob-lester-jr04/laravel-google-calendar

管理Google日历上的事件

2.2.2 2019-02-27 20:36 UTC

This package is auto-updated.

Last update: 2024-09-12 00:24:25 UTC


README

Latest Version on Packagist Software License Build Status Quality Score StyleCI Total Downloads

本包使与Google日历协同工作变得轻松简单。设置完成后,您可以执行以下操作:

use Lester\GoogleCalendar\Event;

//create a new event
$event = new Event;

$event->name = 'A new event';
$event->startDateTime = Carbon\Carbon::now();
$event->endDateTime = Carbon\Carbon::now()->addHour();
$event->addAttendee(['email' => 'youremail@gmail.com']);
$event->addAttendee(['email' => 'anotherEmail@gmail.com']);

$event->save();

// get all future events on a calendar
$events = Event::get();

// update existing event
$firstEvent = $events->first();
$firstEvent->name = 'updated name';
$firstEvent->save();

$firstEvent->update(['name' => 'updated again']);

// create a new event
Event::create([
   'name' => 'A new event',
   'startDateTime' => Carbon\Carbon::now(),
   'endDateTime' => Carbon\Carbon::now()->addHour(),
]);

// delete an event
$event->delete();

安装

您可以通过composer安装此包

composer require rob-lester-jr04/laravel-google-calendar

接下来,必须注册服务提供者

'providers' => [
    ...
    Lester\GoogleCalendar\GoogleCalendarServiceProvider::class,
];

可选地,必须注册Lester\GoogleCalendar\GoogleCalendarFacade

'aliases' => [
	...
    'GoogleCalendar' => Lester\GoogleCalendar\GoogleCalendarFacade::class,
    ...
]

您必须使用以下命令发布配置

php artisan vendor:publish --provider="Lester\GoogleCalendar\GoogleCalendarServiceProvider"

这将发布一个名为google-calendar.php的文件到您的config目录,内容如下

return [
    /*
     * Path to the json file containing the credentials.
     */
    'service_account_credentials_json' => storage_path('app/google-calendar/service-account-credentials.json'),

    /*
     *  The id of the Google Calendar that will be used by default.
     */
    'calendar_id' => env('GOOGLE_CALENDAR_ID'),
];

如何获取与Google日历通信的凭据

首先,您需要获取一些用于使用Google API的凭据。我假设您已经创建了一个Google账户并已登录。前往Google API控制台并在页眉处点击“选择项目”。

1

接下来,我们必须指定项目可以使用的API。在可用的API列表中点击“Google Calendar API”。在下一屏幕上点击“启用”。

2

现在,您已经创建了一个可以访问日历API的项目,是时候下载包含这些凭据的文件了。在侧边栏中点击“凭据”。您将想要创建一个“服务帐户密钥”。

3

在下一屏幕上,您可以为服务帐户命名。您可以命名它任何您想要的名称。在服务帐户ID中,您会看到一个电子邮件地址。我们将在本指南的稍后部分使用此电子邮件地址。选择“JSON”作为密钥类型,然后点击“创建”以下载JSON文件。

4

请将json保存到Laravel项目的指定位置,位置在包的配置文件的service_account_credentials_json键中。由于json文件可能包含敏感信息,我不建议将其提交到git仓库。

现在,API站点上的所有设置都已就绪,我们还需要在Google日历网站上配置一些东西。前往Google日历并查看您将通过PHP操作的那个日历的设置。在“共享此日历”选项卡中,添加在API站点创建凭据时显示的服务帐户ID。

5

打开“日历详情”选项卡以查看日历的ID。您需要在配置文件中指定该ID。

6

用法

获取事件

您可以通过调用Event::get();来获取所有事件,这将返回未来一年的所有事件。事件以Lester\GoogleCalendar\Event对象的形式出现。

函数的完整签名如下

public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null): Collection

您可以在$queryParameters中传递的参数列在Google Calendar API文档中关于list的文档上。

访问事件的开始和结束日期

您可以使用这些getter来检索作为Carbon实例的开始和结束日期

$events = Event::get();

$events[0]->startDate;
$events[0]->startDateTime;
$events[0]->endDate;
$events[0]->endDateTime;

创建事件

您可以直接创建一个Lester\GoogleCalendar\Event-对象

$event = new Event;

$event->name = 'A new event';
$event->startDateTime = Carbon\Carbon::now();
$event->endDateTime = Carbon\Carbon::now()->addHour();

$event->save();

您也可以静态地调用create

Event::create([
   'name' => 'A new event',
   'startDateTime' => Carbon\Carbon::now(),
   'endDateTime' => Carbon\Carbon::now()->addHour(),
]);

这将创建一个具有特定开始和结束时间的活动。如果您想创建一个全天活动,您必须使用startDateendDate而不是startDateTimeendDateTime

$event = new Event;

$event->name = 'A new full day event';
$event->startDate = Carbon\Carbon::now();
$event->endDate = Carbon\Carbon::now()->addDay();

$event->save();

获取单个事件

Google为每个单独的事件分配一个唯一的ID。您可以通过调用get方法获取事件,并在Lester\GoogleCalendar\Event-对象上获取id属性来获取此ID

// get the id of the first upcoming event in the calendar.
$eventId = Event::get()->first()->id;

您可以使用此ID从谷歌获取单个事件

Event::find($eventId);

更新事件

很简单,只需更改一些属性并调用 save()

$event = Event::find($eventId);

$event->name = 'My updated title';
$event->save();

或者您可以使用更新方法

$event = Event::find($eventId)

$event->update(['name' => 'My updated title']);

删除事件

就这么简单!

$event = Event::find($eventId);

$event->delete();

限制

Google日历API提供了许多选项。此包不支持所有这些选项。例如,使用此包无法正确管理重复事件。如果您坚持使用名称和日期创建事件,应该没有问题。

从v1升级到v2

v1v2 之间的主要区别在于,底层使用的是Google API v2而不是v1。以下是升级所需的步骤:

  • 将配置文件重命名为 google-calendar
  • 在配置文件中将 client_secret_json 键重命名为 service_account_credentials_json

变更日志

有关最近更改的更多信息,请参阅变更日志

测试

composer test

贡献

有关详细信息,请参阅贡献指南

安全

如果您发现任何安全相关的问题,请通过robertlesterjr@mac.com发送电子邮件,而不是使用问题跟踪器。

鸣谢

这是一个基于Spatie的谷歌日历包的分支。

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件