php - 使用服务帐户插入 Google 日历条目

标签 php google-calendar-api google-api-php-client

我正在尝试使用服务帐户在 Google 日历上创建条目。我真的很接近这一点,但最后一行行不通。当我让它运行时,我得到一个 500 Internal Service Error。否则,程序将无错误地运行,无论其值(value)如何。

可以找到Calendar.php 文件内容here .我尝试调用的 insert() 方法从该文件的第 1455 行开始。

<?php

function calendarize ($title, $desc, $ev_date, $cal_id) {

    session_start();

    /************************************************
    Make an API request authenticated with a service
    account.
    ************************************************/
    set_include_path( '../google-api-php-client/src/');

    require_once 'Google/Client.php';
    require_once 'Google/Service/Calendar.php';

    // (not real keys)
    $client_id = '843319906820-jarm3f5ctbtjj9b7lp5qdcqal54p1he6.apps.googleusercontent.com';
    $service_account_name = '843319906820-jarm3f5ctbtjj7b7lp5qdcqal54p1he6@developer.gserviceaccount.com';
    $key_file_location = '../google-api-php-client/calendar-249226a7a27a.p12';

//    echo pageHeader("Service Account Access");
    if (!strlen($service_account_name) || !strlen($key_file_location))
        echo missingServiceAccountDetailsWarning();

    $client = new Google_Client();
    $client->setApplicationName("xxxx Add Google Calendar Entries");

    if (isset($_SESSION['service_token'])) {
        $client->setAccessToken($_SESSION['service_token']);
    }

    $key = file_get_contents($key_file_location);
    $cred = new Google_Auth_AssertionCredentials(
        $service_account_name, 
        array('https://www.googleapis.com/auth/calendar'), 
        $key
    );
    $client->setAssertionCredentials($cred);
    if($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }
    $_SESSION['service_token'] = $client->getAccessToken();

    // Prior to this, the code has mostly come from Google's example
    //     google-api-php-client / examples / service-account.php
    // and relates to getting the access tokens. 

    // The rest of this is about setting up the calendar entry.
    //Set the Event data
    $event = new Google_Service_Calendar_Event();
    $event->setSummary($title);
    $event->setDescription($desc);

    $start = new Google_Service_Calendar_EventDateTime();
    $start->setDate($ev_date);
    $event->setStart($start);

    $end = new Google_Service_Calendar_EventDateTime();
    $end->setDate($ev_date);     
    $event->setEnd($end);

    $calendarService = new Google_Service_Calendar($client);
    $calendarList = $calendarService->calendarList;

    $events = $calendarService->events;

    // if I leave this line, my code won't crash (but it won't do anything, either)
    //echo "here"; die(); 

    $events.insert($cal_id, $event, false);
} 

?>

最佳答案

我想通了。由于我没有看到任何通过 API v3 使用服务帐户的完整示例,因此我将发布我的完整解决方案以供引用。但是,除了实现代码之外,您还需要做一些事情:

1) 你需要去Google Developer's console并将您的帐户标记为“服务帐户”。这将使它有别于 Web 应用程序。重要的区别是在添加事件之前不会提示任何人登录他们的帐户,因为该帐户将属于您的应用程序,而不是最终用户。有关详细信息,请参阅此 article ,从第 5 页开始。

2) 您需要创建一个公钥/私钥对。在开发人员控制台中,单击凭据。在您的服务帐户下,单击“生成新的 P12 key ”。您需要将其存储在某个地方。该文件位置成为下面代码中的 $key_file_location 变量字符串。

3) 同样从开发人员的控制台,您需要启用 Calendar API。在您的项目的左边距中,您会看到 APIs。选择它并找到 Calendar API。单击它,接受服务条款,并验证它现在显示在 Enabled APIs 下,状态为 On

4) 在您要向其中添加事件的 Google 日历中,在设置下单击日历设置,然后单击顶部的“共享此日历”。在“人员”字段的“与特定人员共享”下,粘贴服务帐户凭据中的电子邮件地址。将权限设置更改为“更改事件”。不要忘记保存更改。

然后,在某处实现这段代码。

评论是否有混淆或遗漏的地方。祝你好运!

<?php

function calendarize ($title, $desc, $ev_date, $cal_id) {

    session_start();

    /************************************************
    Make an API request authenticated with a service
    account.
    ************************************************/
    set_include_path( '../google-api-php-client/src/');

    require_once 'Google/Client.php';
    require_once 'Google/Service/Calendar.php';

    //obviously, insert your own credentials from the service account in the Google Developer's console
    $client_id = '843319906820-xxxxxxxxxxxxxxxxxxxdcqal54p1he6.apps.googleusercontent.com';
    $service_account_name = '843319906820-xxxxxxxxxxxxxxxxxxxdcqal54p1he6@developer.gserviceaccount.com';
    $key_file_location = '../google-api-php-client/calendar-xxxxxxxxxxxx.p12';

    if (!strlen($service_account_name) || !strlen($key_file_location))
        echo missingServiceAccountDetailsWarning();

    $client = new Google_Client();
    $client->setApplicationName("Whatever the name of your app is");

    if (isset($_SESSION['service_token'])) {
        $client->setAccessToken($_SESSION['service_token']);
    }

    $key = file_get_contents($key_file_location);
    $cred = new Google_Auth_AssertionCredentials(
        $service_account_name, 
        array('https://www.googleapis.com/auth/calendar'), 
        $key
    );
    $client->setAssertionCredentials($cred);
    if($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }
    $_SESSION['service_token'] = $client->getAccessToken();

    $calendarService = new Google_Service_Calendar($client);
    $calendarList = $calendarService->calendarList;

    //Set the Event data
    $event = new Google_Service_Calendar_Event();
    $event->setSummary($title);
    $event->setDescription($desc);

    $start = new Google_Service_Calendar_EventDateTime();
    $start->setDateTime($ev_date);
    $event->setStart($start);

    $end = new Google_Service_Calendar_EventDateTime();
    $end->setDateTime($ev_date);
    $event->setEnd($end);

    $createdEvent = $calendarService->events->insert($cal_id, $event);

    echo $createdEvent->getId();
} 

?>

一些有用的资源:
Github example for service accounts
Google Developers Console for inserting events in API v3
Using OAuth 2.0 to Access Google APIs

关于php - 使用服务帐户插入 Google 日历条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26064095/

相关文章:

php - 如何按条件过滤内爆结果?

php - 检索模板 joomla 2.5 中的模块参数设置

go - 在 Go 中连接 Google Calendar API 的凭据

jquery - fullcalendar jQuery - 可以从 Google 日历事件中检索描述吗?

php - 如何向Google Analytics API请求添加维度

PHP - 在我的 google plus 页面流上发布文章

php - symfony2 应用程序,流浪者和 Ant : stty: standard input: Invalid argument

php - 连接问题和 2 个确切的列名

c# - 如何使用 Google API 版本 2 在 Google Apps 日历中添加 "Attachment"?

google-api - 通过具有特定页面设置(边距、方向)的 Drive API 创建文档