大家好~
接着一起来介接 API 吧!
今天先从 Google Calendar API 开始~
上图撷取自 Google Calendar API 的官方文件,
官方将我们能使用的资源分成八个类型,
该篇主要会以使用 Events 的 CRUD 为主,
其他资源的使用,
不会介绍到喔~
使用 Composer 下载 google-api-php-client。
composer require google/apiclient:^2.0
据官方表示,
该 Repo 内有超过200个的 Google 服务,
如果想清除其他不会用到的部分,
可以藉由修改 composer.json
来保留
{
"require": {
"google/apiclient": "^2.11"
},
"scripts": {
"pre-autoload-dump": "Google\\Task\\Composer::cleanup"
},
"extra": {
"google/apiclient-services": [
"Calendar",
"Sheets"
]
}
}
extra
内放你要保留的服务。
rm -r vendor/google/apiclient-services
删除 apiclient-services 的资料夹,
composer update
再用 composer update
重新下载,
这样就会只保留在 extra
内指定的那些服务罗。
在 GCP 上新增专案。
建立完专案後,
启用 Google Calendar API。
之後会看到如下图之提醒。
然後点击同列右侧的建立凭证
。
下一步
建立金钥
选择建立 JSON 类型的金钥。
专案的新增与金钥建立差不多就是这样的流程,
基本上只要跟着网站给的流程走就能完成罗!
也可以参考下面这篇官方文件:
Create a project and enable the API
在 Google Calendar 页面新增一个日历。
对 ironman2021
这个日历选择设定和共用
。
将刚刚建立的服务帐户
加进共用日历。
等等会用到的 日历ID
也在这个页面喔~
金钥放专案内的话,
记得要加进 .gitignore
喔~
另外下面范例是把 日历ID
与 金钥位置
加到 .env
。
先实例 Client 和 Calendar 吧~
use Google\Client;
use Google\Service\Calendar;
use Google\Service\Calendar\Event;
use Google\Service\Calendar\EventDateTime;
protected $client;
protected $googleCalendar;
protected $calendarId;
public function __construct()
{
$this->client = new Client();
$this->client->setAuthConfig(env('CLIENT_CREDENTIALS_PATH'));
$this->client->addScope(Calendar::CALENDAR_EVENTS);
$this->googleCalendar = new Calendar($this->client);
$this->calendarId = env('GOOGLE_CALENDAR_ID');
}
public function insert(Request $request)
{
$calendarEvent = new Event();
$calendarEvent->setSummary($request->summary);
$calendarEvent->setDescription($request->description);
$startTime = new EventDateTime();
$startTime->setDateTime($request->startTime);
$endTime = new EventDateTime();
$endTime->setDateTime($request->endTime);
$calendarEvent->setStart($startTime);
$calendarEvent->setEnd($endTime);
$response = $this->googleCalendar->events->insert($this->calendarId, $calendarEvent);
return $response->getHtmlLink();
}
event 在成功建立後会返回一个 Events resource,
另外 DateTim 的时间格式要用 RFC3339 喔~
用 Postman 测试一下。
成功~
那麽明天再来继续 Events 的 Read、Update、Delete 吧~
大家明天见!
若文章有任何问题,
还请大家不吝赐教!
<<: Day 25 | Flutter 路由管理套件 - auto_route
>>: 大共享时代系列_022_Twitch Plays (衆人同时用弹幕控制一个游戏角色)
资料型别 JavaScript 的型别主要可以分成基本型别 (Primitives)与物件型别 ...
我真的没想到渐层可以写这麽多篇XD 本来预期是一篇就可以结束... 结果开始研究发现,不得了阿这个属...
Raid可以理解为是将多个硬碟组合在一起,利用虚拟储存技术,形成一个硬碟阵列,用来提升储存空间和制造...
this: 存在全域的执行环境,也存在函式所开启的执行环境 var myname = 'Tom'; ...
我只是一个小新手 因为有使用者需求,当时也找很久想说纪录一下 下拉清单,要多一条线 像这样 当初在国...