目标
我正在尝试将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]
由于404
是page not found
响应,因此它似乎在调用错误的api端点。