大家好~
接着一起来介接 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 (衆人同时用弹幕控制一个游戏角色)
正文 今天最後一天了,当初因为没存稿的关系,加上很多主题想写,但又没把握能写好,所以不知道能不能顺利...
今天是最後一篇,也是第三十天了,各位每一天都跟我一样有练习一题,这样三十天已经练习接近三十题了,虽然...
前言 昨天讲到要如何建立model,今天来讲要如何训练以及预测 编译模型 建立完模型之後,必须呼叫c...
--哇.这是什麽资料库,我怎麽没这个语法. drop table if exists delme c...
1.文字工具 (档案12,影片Ps1) 左框 (1)文字工具 (1)文字: a.段落文字:拉框,拉大...