Google Client for Calendar服务以404响应 目标我做了什么错误

目标

我正在尝试将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 accountdocs 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]

由于404page not found响应,因此它似乎在调用错误的api端点。

日历ID
为了清楚起见,这是我获取日历ID的位置

Google Client for Calendar服务以404响应
      
    目标我做了什么错误

henryin 回答:Google Client for Calendar服务以404响应 目标我做了什么错误

据我所知,服务帐户没有自己的日历,因此需要授予它们访问特定组织的权限,以便创建/读取事件。

如果这是用于内部应用程序的(并且您是组织的管理员),则可以按照Delegating Authority下的步骤授予服务帐户访问您组织的日历的权限:

然后,G Suite域的超级管理员必须完成以下步骤:

  1. 从您的G Suite域的Admin console转到主菜单菜单>安全> API控件。
  2. 在“域范围委派”窗格中,选择“管理域范围委派”。 点击添加新内容。
  3. 在“客户ID”字段中,输入服务帐户的客户ID。您可以在Service accounts page中找到服务帐户的客户ID。
  4. 在OAuth范围(以逗号分隔)字段中,输入应授予您的应用程序访问权限的范围列表。例如,如果您的应用程序需要在域范围内对Google Drive API和Google Calendar API的完全访问权限,请输入:https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/calendar
  5. 点击授权。

您还需要按照以下步骤在Preparing to make an authorized API call下模拟特定用户,以便为有权访问相关日历的用户检索令牌。

如果这是针对打算由多个组织安装的应用程序,则应考虑使用托管的OAuth服务,例如Xkit(我工作的地方 )。特别是Xkit,有一个Google Calendar Service Account connector,可以从开发人员那里摘走所有这些步骤(包括IT管理员的分步指南),以便在一个API调用中为您提供有效的访问令牌。>

请注意,您引用的服务帐户示例使用了Books API中的公共数据,因此不需要用户授权,只需要将自己标识为应用程序即可。

本文链接:https://www.f2er.com/1365874.html

大家都在问