php Google日历API -创建活动+ Google Meet链接

dba5bblo  于 2023-04-04  发布在  PHP
关注(0)|答案(1)|浏览(217)

我知道关于这个问题有很多要求;但是他们似乎都没有回答我的问题。为了自动生成Google日历约会,包括自动生成相应的Google Meet链接,我做了以下事情:
1.创建Google Cloud项目
1.已为相关帐户启用Google Calendar API
1.创建新的Google服务帐户
1.获取该服务帐户的电子邮件地址,并将其添加到Google日历的设置下,以允许对该电子邮件进行更改/管理访问的权限。
1.下载了最新版本的google API客户端(https://packagist.org/packages/google/apiclient)。
1.转到Google Service Accounts〉Keys〉Generated new key file as JSON,let's say access.json .
1.在PHP中实现用于生成日历API约会的代码,包括最近的补丁(https://developers.google.com/calendar/api/releases?hl = de#september_07_2020):

putenv('GOOGLE_APPLICATION_CREDENTIALS=my/key/data/directoy/access.json');
        $client = new Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope(Google_Service_Calendar::CALENDAR);
        $client->setSubject("{address_explained_below}");
        
        // Set up the Calendar API service
        $service = new Google_Service_Calendar($client);
        
        // Create a new event on Google Calendar
        $event = new Google_Service_Calendar_Event(
            [
                'summary'        => 'Meeting',
                'start'          => [
                    'dateTime' => '2023-04-07T10:00:00-07:00',
                    // Replace with your desired start time
                    'timeZone' => 'America/Los_Angeles',
                    // Replace with your desired timezone
                ],
                'end'            => [
                    'dateTime' => '2023-04-07T11:00:00-07:00',
                    // Replace with your desired end time
                    'timeZone' => 'America/Los_Angeles',
                    // Replace with your desired timezone
                ]
            ]
        );
        
        // Set the event's conference data to use Google Meet
        $conferenceRequest = new Google_Service_Calendar_CreateConferenceRequest();
    
        $conferenceRequest->setRequestId(uniqid());
        
        $solution_key = new Google_Service_Calendar_ConferenceSolutionKey();
        $solution_key->setType("hangoutsMeet");
        $conferenceRequest->setConferenceSolutionKey($solution_key);
        
        $conference = new Google_Service_Calendar_ConferenceData();
        
        $conference->setCreateRequest($conferenceRequest);
        
        $event->setConferenceData($conference);
        
        // Insert the event into the user's calendar
        $calendarId = 'myCalendarsID';
        $event = $service->events->insert(
            $calendarId,
            $event,
            [ 'conferenceDataVersion' => 1 ]
        );
        
        var_dump($event);
        
        // Retrieve the generated Google Meet link from the event's conference data
        $meetLink = $event->getHangoutLink();

我真的试着听从这个论坛上所有其他帖子关于这个问题的建议;但是什么都没做,我现在得到了一个400 Bad Request错误,不管我设置的是typehangoutsMeet还是其他什么),都是Invalid conference type value.。我错过了什么?我遇到了this;这可能是特定于库的,并且在不使用库的情况下实现原始HTTP REST API调用会更好吗?

更新日期:

在使用Google提供的Test Feature在这里触发此请求后:
GET https://www.googleapis.com/calendar/v3/calendars/{calendarId}
我确实可以确认hangoutsMeetallowedConferenceSolutionTypes之内。

{
 "kind": "calendar#calendar",
 "etag": "secret",
 "id": "secret",
 "summary": "secret",
 "timeZone": "secret",
 "conferenceProperties": {
  "allowedConferenceSolutionTypes": [
   "hangoutsMeet"
  ]
 }
}

所以我想,根据@洛雷纳Gomez(谢谢!)的评论,我必须“向我的服务帐户授予域范围的委托,并模拟具有该日历权限的用户”,即使我不太清楚这实际上意味着什么/做了什么。
关于域范围的委托,我现在已经为注册的服务帐户做了,如这里所指定的。这似乎起作用了,尽管我在相关服务帐户下的“IAM和管理”屏幕内的服务帐户面板中看不到任何差异。这个委托可能需要一些时间才能生效吗?
我一直是digging further,显然这行代码在这里:
$client->setSubject($user_to_impersonate);
用于模拟具有该日历权限的用户。但是这里$user_to_impersonate的值应该是什么呢?我已经提供了服务帐户的电子邮件地址,我为https://www.googleapis.com/auth/calendar范围授予了域范围的委托,在这种情况下,我仍然得到:

Google_Service_Exception: { 
  "error": { 
    "errors": [ 
      { 
        "domain": "global", 
        "reason": "invalid", 
        "message": "Invalid conference type value." 
      } 
    ], 
    "code": 400, 
    "message": "Invalid conference type value." 
  } 
}

然后,我用实际拥有日历的Google帐户的电子邮件地址进行了尝试,结果是:

Google_Service_Exception: { 
  "error": "unauthorized_client", 
  "error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested." 
} 

Error code: 401

然后,我尝试使用被授予域范围委托的服务帐户的用户ID,这时我得到:

Caught Google_Service_Exception: { 
  "error": "invalid_request", 
  "error_description": "Invalid principal" 
} 

Error code: 400

所以我不明白,我还缺少什么?我已经将上面的代码更新到现在的状态(字符串address_explained_below被替换为上面提到的三次尝试)。
我现在已经做了最后一次尝试,我已经创建了一个新的服务帐户,启用了域范围的委托,生成了访问密钥,并将它们上传到服务器。然后,当运行上面的代码与新创建的服务帐户的电子邮件地址,我得到了一个404 Not found错误。然后,我去了有关日历的谷歌日历设置,并在Share with specific people or groups下添加了服务帐户的电子邮件地址(我已经翻译了那部分;这是您可以授予其他人访问您的日历的部分)。在这样做并重新运行上面的脚本之后,我现在再次获得400 Error Invalid conference type value
我唯一能想到的是putenv调用设置变量的值(使用getenv检查)添加到保存凭据的.json文件的绝对路径,从服务器的根。这是意图吗?如何读取/访问凭证?只是问一下,因为.json文件在0500目录中,而.json文件本身也是0500,但这应该没问题吧?好吧,我卡住了。
我有一个想法:您在Google Workspace-Domain中验证的域是否必须是您发送API请求/运行上述代码的域?因为目前情况并非如此...

k5ifujac

k5ifujac1#

在代码中尝试这样设置conff数据,如下所示,使用API客户端for PHP生成带有自动GoogleMeet链接的GoogleCalendar约会

<?php

// Include the Google API client library
require __DIR__ . '/vendor/autoload.php';

// Set the path to your Google Cloud service account key file
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account_key.json');

// Create a new Google API client instance
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Calendar::CALENDAR);

// Set up the Google Calendar API service
$service = new Google_Service_Calendar($client);

// Set the start and end times of the appointment
$startDateTime = new DateTime('2023-04-07T10:00:00-07:00', new DateTimeZone('America/Los_Angeles'));
$endDateTime = new DateTime('2023-04-07T11:00:00-07:00', new DateTimeZone('America/Los_Angeles'));

// Create a new Google Calendar event with the specified start and end times
$event = new Google_Service_Calendar_Event([
    'summary' => 'Meeting',
    'start' => [
        'dateTime' => $startDateTime->format(DateTime::RFC3339),
        'timeZone' => $startDateTime->getTimezone()->getName(),
    ],
    'end' => [
        'dateTime' => $endDateTime->format(DateTime::RFC3339),
        'timeZone' => $endDateTime->getTimezone()->getName(),
    ],
]);

// Set the event's conference data to use Google Meet
$conferenceRequest = new Google_Service_Calendar_CreateConferenceRequest();
$conferenceRequest->setRequestId(uniqid());
$conferenceSolutionKey = new Google_Service_Calendar_ConferenceSolutionKey();
$conferenceSolutionKey->setType('hangoutsMeet');
$conferenceRequest->setConferenceSolutionKey($conferenceSolutionKey);
$conferenceData = new Google_Service_Calendar_ConferenceData();
$conferenceData->setCreateRequest($conferenceRequest);
$event->setConferenceData($conferenceData);

// Insert the event into the user's calendar
$calendarId = 'primary'; // Change this to the calendar ID you want to use
$event = $service->events->insert($calendarId, $event, ['conferenceDataVersion' => 1]);

// Retrieve the generated Google Meet link from the event's conference data
$meetLink = $event->getHangoutLink();

// Output the generated Google Meet link
echo 'Google Meet link: ' . $meetLink;

注意事项:
1.请更换/path/to/service_account_key.json
1.另请输入$calendarId值。
您必须安装Dipendency .. by composer require google/apiclient

相关问题