Google Calendar API - 创建活动 + Google Meet 链接

问题描述 投票:0回答:1

我知道有很多关于这个问题的请求;但他们似乎都没有回答我的问题。为了自动生成 Google 日历约会,包括自动生成相应的 Google Meet Link,我做了以下工作:

  1. 创建谷歌云项目
  2. 为相关帐户启用 Google Calendar API
  3. 已创建新的 Google 服务帐户
  4. 获取该服务帐户的电子邮件地址,并将其添加到 Google 日历的设置下,以允许对该电子邮件进行更改/管理访问的权限。
  5. 下载了最新版本的 google api 客户端(https://packagist.org/packages/google/apiclient)。
  6. 转到 Google 服务帐户 > 密钥 > 生成新的密钥文件作为
    JSON
    ,比方说
    access.json
    .
  7. 在 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);
        
        // 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
错误,无论我设置的
type
hangoutsMeet
或其他什么),说
Invalid conference type value.
。我错过了什么?我遇到过这个;这可能是特定于库的,最好在不使用库的情况下实现原始 HTTP REST API 调用?

php google-cloud-platform google-calendar-api
1个回答
0
投票

尝试使用您的代码来设置 conf 数据,如下所示,使用适用于 PHP 的 Google API 客户端生成带有自动 Google Meet 链接的 Google 日历约会

<?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. plz改变
    /path/to/service_account_key.json
  2. 同时输入
    $calendarId
    值。

您必须通过

composer require google/apiclient

安装Dipendency ..
© www.soinside.com 2019 - 2024. All rights reserved.