首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让我的服务器使用PHP客户端库将事件添加到google日历?

如何让我的服务器使用PHP客户端库将事件添加到google日历?
EN

Stack Overflow用户
提问于 2014-12-01 15:17:06
回答 2查看 2.9K关注 0票数 1

我几乎没有使用google的经验,我发现很难找到关于某些主题的相关信息。

我要做的是创建一个不需要用户登录信息的日历。我希望我的服务器根据数据库中的信息将事件添加到日历中。

我已经进入了我的google日历,创建了一个新的日历,并编写了一些代码来将事件添加到日历中。然后我使用谷歌提供的iframe代码将日历嵌入到我的站点中。

我遇到的问题是,服务器希望用户以我的身份登录,以便将事件添加到日历中。因为服务器是添加事件的服务器,所以我不太确定如何解决这个问题。我知道我需要创建/使用一个服务帐户,这样我的服务器就可以“委托域范围内的权限”。,但我不知道如何使用客户端库进行此操作。谷歌没有提供任何例子。我必须下载一个p12文件,服务器需要该文件进行这些api调用,但我不知道如何使用客户端库指向该文件。是否可以使用php客户端库连接到服务帐户进行这些api调用?如果是这样的话,是怎么做的?

任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-30 09:21:10

我也遇到了同样的问题,并在这里找到了解决方案(大致是这样):使用服务帐户插入Google条目

第一件事是正确设置你的谷歌日历(见上面提到的文章,很好地描述它),然后从这里下载日历的API代码-- https://github.com/google/google-api-php-client --这是页面右侧的下载ZIP链接。

下面是一些适用于我的示例代码(私钥在适当情况下被xxxx替换,但否则它正是我所使用的)。

我现在正试图找出如何阅读和清除谷歌日历,这证明了更多的挑战!

代码语言:javascript
运行
复制
<?php
//
// built from example at:
// https://stackoverflow.com/questions/26064095/inserting-google-calendar-entries-with-service-account
//
$startdate = new DateTime('2015-01-29 10:00', new DateTimeZone('Europe/London'));
$startdate = $startdate->format('c');
$enddate = new DateTime('2015-01-29 10:00', new DateTimeZone('Europe/London'));
$enddate = $enddate->format('c');
//
// call function to add one event to the calendar (xxxxxxxxxxx@googlemail.com = the calendar owner)
//
calendarize('Test Appointment','Test appt description',$startdate,$enddate,'xxxxxxxxxxx@googlemail.com');

//-----------------------------------------------//
// funtion to add an event to my Google calendar //
//-----------------------------------------------//
function calendarize ($title, $desc, $start_ev_datetime, $end_ev_datetime, $cal_id) {
    session_start();
    require_once '../google-api-php-client-master/autoload.php';
    //Google credentials
    $client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
    $service_account_name = 'xxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com';
    $key_file_location = '../google-api-php-client-master/API Project-xxxxxxxxxx.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()) {
        try {
          $client->getAuth()->refreshTokenWithAssertion($cred);
        } catch (Exception $e) {
          var_dump($e->getMessage());
        }
    }
    $_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($start_ev_datetime);
    $start->setTimeZone('Europe/London');
    $event->setStart($start);
    $end = new Google_Service_Calendar_EventDateTime();
    $end->setDateTime($end_ev_datetime);
    $end->setTimeZone('Europe/London');
    $event->setEnd($end);
    try {
      $createdEvent = $calendarService->events->insert($cal_id, $event);
    } catch (Exception $e) {
      var_dump($e->getMessage());
    }
    echo 'Event Successfully Added with ID: '.$createdEvent->getId();
}
?>
票数 2
EN

Stack Overflow用户

发布于 2014-12-02 13:46:59

使用OAuth2登录并赋予服务器访问日历帐户的权限是非常简单的,并且有很好的文档记录(使用google客户端)。如果请求脱机访问并存储刷新令牌,则可以在任何时候登录"as you“服务器端。

请参阅Google帐户身份验证和授权:脱机访问

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27231460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档