如何让服务器使用PHP客户端库向google日历添加事件?

如何让服务器使用PHP客户端库向google日历添加事件?,php,google-api,google-calendar-api,google-api-php-client,Php,Google Api,Google Calendar Api,Google Api Php Client,我很少有使用谷歌api的经验,我发现很难找到某些主题的相关信息 我要做的是创建一个不需要用户登录信息的日历。我希望我的服务器根据数据库中的信息向日历中添加事件 我已经进入了我的谷歌日历,创建了一个新的日历,并编写了一些代码将事件添加到日历中。然后,我使用谷歌提供的iframe代码将日历嵌入到我的网站中 我遇到的问题是,服务器希望用户以我的身份登录,以便向日历中添加事件。由于服务器是添加事件的服务器,因此我不确定如何解决此问题,但我不知道如何使用PHP客户端库来实现这一点。谷歌没有提供任何例子。我

我很少有使用谷歌api的经验,我发现很难找到某些主题的相关信息

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

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

我遇到的问题是,服务器希望用户以我的身份登录,以便向日历中添加事件。由于服务器是添加事件的服务器,因此我不确定如何解决此问题,但我不知道如何使用PHP客户端库来实现这一点。谷歌没有提供任何例子。我不得不下载一个p12文件,我的服务器需要它来进行这些api调用,但我不确定如何使用客户端库指向该文件。是否可以使用php客户机库连接到服务帐户以进行这些api调用?如果是,怎么做


非常感谢您的帮助。

使用OAuth2登录并授予您的服务器访问您的日历帐户的权限非常简单,而且有很好的文档记录(使用google api php客户端)。如果您请求脱机访问并存储刷新令牌,您可以随时以“您的身份”登录服务器端


参见

我也遇到了同样的问题,并在这里找到了解决方案(以一种迂回的方式):

第一件事是正确设置您的谷歌日历(请参阅上面提到的文章,其中对它进行了很好的描述),然后从这里下载日历的API代码 这是该页面右侧的下载ZIP链接

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

我现在正试图找到如何阅读和清除谷歌日历,这是证明更具挑战性

<?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();
}
?>


可能是因为我对php和谷歌api还不熟悉,但我在工作中遇到了困难。我已经按照设置脱机访问的说明进行了操作,并且不断收到一条错误消息,说授权令牌已过期,刷新令牌不可用。我会继续努力的。谢谢你的帮助!我发布了一些代码。但现在我正在尝试使用服务帐户的另一种方法,但也遇到了问题,即使它更容易理解:/我完全按照您所说的做了,但它给出了致命错误:找不到类“Google_Auth_AssertionCredentials”。我通过composer安装了google api v2。@sumit这是您需要的: