ZF2中的Google Calendar V3服务帐户使用情况


Google Calendar V3 service account usage in ZF2

我知道我不是第一个问这个问题的人,但我已经找了几天了,似乎找不到解决问题的办法。因此,问题是我需要用谷歌帐户设置我的应用程序,每当用户完成一个操作时,应用程序都会在应用程序的谷歌日历中添加一个事件,而无需征得用户的同意。我需要在代码中完成应用程序帐户的所有身份验证。我已经做了一些搜索,我认为通过服务帐户进行谷歌服务器到服务器的通信是可行的。https://developers.google.com/accounts/docs/OAuth2ServiceAccount

问题是我不知道如何设置客户端,获取访问令牌并用它调用API。到目前为止,我已经使用composer:安装了Google API

{
   "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.3.*",
        "dlu/dlutwbootstrap": "dev-master",
        "google/apiclient": "1.0.*@beta"
    }
}

我还从谷歌开发者控制台创建了一个服务帐户,并获得了客户Id、电子邮件等

$client = new 'Google_Client();
    $clientId = '<MY ID>';
    $service_email = '<THE EMAIL>';
    $myMail = '<MY OTHER MAIL>';
    $keyFile = './data/<NAME OF FILE>';
    $client->setClientId($clientId);
    $client->setApplicationName('Timesheet');
    $key = file_get_contents($keyFile);
    $client->setAssertionCredentials(new Google_AssertionCredentials(
        $service_email,
        array('https://www.googleapis.com/auth/calendar'),
        $key));
    $client->getRequestToken();
    $calendar = new 'Google_Service_Calendar($client);
    $event = new Event();
    $event->setSummary('Appointment');
    $event->setLocation('Somewhere');
    $start = new EventDateTime();
    $start->setDateTime('2014-11-05T10:00:00.000-07:00');
    $event->setStart($start);
    $end = new EventDateTime();
    $end->setDateTime('2014-11-05T10:25:00.000-07:00');
    $event->setEnd($end);
    $attendee1 = new EventAttendee();
    $attendee1->setEmail('<EMAIL ONE>');
    $attendee2 = new EventAttendee();
    $attendee2->setEmail('<EMAIL TWO>');
    $attendees = array($attendee1, $attendee2);
    $event->attendees = $attendees;
    $createdEvent = $calendar->events->insert('<CALENDAR TO INSERT EVENT INTO>', $event);

如果有人能指出我缺少什么并指导我正确使用API,我将不胜感激。提前谢谢!

致问候,

Valentin

终于找到了答案!错误出现在插入调用中:

$calendar->events->insert('primary', $event);是正确的代码位。您需要将'primary'作为第一个参数,而不是电子邮件。

希望这能帮助到别人!:)

相关文章: