通过Google API发送邀请时设置“发件人”电子邮件和姓名

时间:2018-07-12 07:04:22

标签: php google-api google-api-php-client

我正在使用Google日历Google API(https://www.googleapis.com/auth/calendar)通过PHP应用程序发送事件邀请。如何设置发送到EventAttendees的通知电子邮件中使用的“发件人”电子邮件和名称?

我尝试为事件设置“创建者”和“组织者”,但这些似乎没有做到。我也尝试过在IAM中创建新的“成员”,但是除Google生成的服务帐户电子邮件外,我无法通过任何其他方式进行身份验证。

我的代码如下:

require_once('src/Google/autoload.php');

$client_id = "redacted@redacted.iam.gserviceaccount.com";
$client_email = 'redacted@redacted.iam.gserviceaccount.com';

$private_key = file_get_contents('redacted.p12');
$scopes = array('https://www.googleapis.com/auth/calendar');
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key
);

$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion();
}

$service = new Google_Service_Calendar($client);

$event = new Google_Service_Calendar_Event();
$event->setSummary('Your requested appointment');
$event->setLocation('Redacted');
$event->sendNotifications=true;

$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2018-07-18T10:00:00.000-05:00');
$event->setStart($start);

$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2018-07-18T11:00:00.000-05:00');
$event->setEnd($end);

$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('redacted@redacted.com');

$attendee2 = new Google_Service_Calendar_EventAttendee();
$attendee2->setEmail('redacted2@redacted.com');

$creator1 = new Google_Service_Calendar_EventCreator();
$creator1->setEmail('info@redacted.com');
$creator1->setDisplayName('Redacted Appointment');
$event->setCreator($creator1);

$org1 = new Google_Service_Calendar_EventOrganizer();
$org1->setEmail('info@redacted.com');
$org1->setDisplayName('Redacted Appointment');
$event->setOrganizer($org1);

// ...
$attendees = array($attendee1,$attendee2,
                   // ...
                  );

$event->attendees = $attendees;
$sendNotifications = array('sendNotifications' => true);
$createdEvent = $service->events->insert('primary', $event, $sendNotifications);

echo $createdEvent->getId();

0 个答案:

没有答案