【发布时间】:2021-02-03 02:57:10
【问题描述】:
目标
我正在尝试将 Google Calendar API 集成到我的公司网站,以便根据某些事件自动创建事件。
事件将被添加到公司日历并显示给用户,所以我不需要用户进行身份验证,我不会在他们自己的日历上工作。
我做了什么
-
按照 Google OAuth 2.0 for Server to Server Applications 文档中的说明创建了 OAuth 2.0 服务帐户
-
创建、下载并添加凭据文件到我的项目,并将其路径添加到环境变量
GOOGLE_APPLICATION_CREDENTIALS; -
关注docs example to authorize my requests using the service account 和docs to create events。
这是我的源代码的简短版本:$client = new Google_Client(); $client->setApplicationName("XXX"); $client->addScope(Google_Service_Calendar::CALENDAR); // "https://www.googleapis.com/auth/calendar" $client->useApplicationDefaultCredentials(); $service = new Google_Service_Calendar($client); $event = new Google_Service_Calendar_Event(/* array with event data */); $calendarId = /* calendar id taken from calendar Settings and Sharing on calendar.google.com*/; $event = $service->events->insert($calendarId, $event);与示例(第 43:52 行)相比,我没有手动检查凭据文件,而是直接查找
useApplicationDefaultCredentials()。
错误
我的代码记录的错误是
[2020-10-19 14:29:19] local.ERROR: {
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "Not Found"
}
],
"code": 404,
"message": "Not Found"
}
}
{"userId":2,"exception":"[object] (Google_Service_Exception(code: 404): {
\"error\": {
\"errors\": [
{
\"domain\": \"global\",
\"reason\": \"notFound\",
\"message\": \"Not Found\"
}
],
\"code\": 404,
\"message\": \"Not Found\"
}
}
at /var/www/vendor/google/apiclient/src/Google/Http/REST.php:123)
[stacktrace]
看起来它调用了错误的 api 端点,因为 404 是 page not found 响应。
【问题讨论】:
标签: php http-status-code-404 google-calendar-api google-client