首页 > 解决方案 > Google Calendar API:无法通过 PHP 中的服务帐户访问获取新创建的活动的 Google Meet 链接

问题描述

我无法让它工作,一直在查看其他几个相关的问题和答案,我认为问题出在我为此使用服务帐户这一事实中。情况是:

这是我的代码:

$client = new Google_Client();

$client->setAuthConfig(xxx);
$client->setScopes("https://www.googleapis.com/auth/calendar");

$service = new Google_Service_Calendar($client);
    
$event = new Google_Service_Calendar_Event(array(
    "description" => "Test", 
    "start" => array(
        "dateTime" => "2021-09-01T09:00",
        "timeZone" => "Europe/Brussels",
    ),
    "end" => array(
        "dateTime" => "2021-09-01T10:00",
        "timeZone" => "Europe/Brussels",
    )           
));

// add google meet link

$solution_key = new Google_Service_Calendar_ConferenceSolutionKey();
$solution_key->setType("hangoutsMeet");
$confrequest = new Google_Service_Calendar_CreateConferenceRequest();
$confrequest->setRequestId("X".time());
$confrequest->setConferenceSolutionKey($solution_key);
$confdata = new Google_Service_Calendar_ConferenceData();
$confdata->setCreateRequest($confrequest);
$event->setConferenceData($confdata);

$event = $service->events->insert($google_calendar_id, $event, array('conferenceDataVersion' => 1));

如果没有参数“array('conferenceDataVersion' => 1)”,则正在创建事件。我几乎可以肯定它与访问权限有关,但我不知道再去哪里看......

标签: phpgoogle-calendar-apigoogle-api-php-clientservice-accountsgoogle-meet

解决方案


“无效的会议类型值”

表示您发送的是会议类型

$solution_key->setType("hangoutsMeet");

您正在使用的日历不允许使用哪个

$google_calendar_id,

尝试运行calendar.get并发送您尝试使用的日历 ID。

要求:

 $service->calendar->get($google_calendar_id)

回复:

{
 "kind": "calendar#calendar",
 "etag": "\"j3g2ipQvsbvz0_YlxYi3Ml2Fd7A\"",
 "id": "xxxxx@gmail.com",
 "summary": "Linda Lawton ",
 "description": "test",
 "timeZone": "Europe/Copenhagen",
 "conferenceProperties": {
  "allowedConferenceSolutionTypes": [
   "hangoutsMeet"
  ]
 }
}

响应中的 allowedConferenceSolutionTypes 将告诉您该日历允许使用哪种类型。

就服务帐户而言,只需确保您设置了域范围的委派,并且您正在委派给域上的用户,这不应该是任何问题。

$client->setSubject($user_to_impersonate);

推荐阅读