我使用Google API的经验很少,我发现很难找到某些主题的相关信息。
我正在尝试做的是创建一个不需要用户登录信息的日历。我希望服务器根据数据库中的信息将事件添加到日历中。
我进入了Google日历,创建了一个新日历,并编写了一些代码将事件添加到日历中。然后,我使用Google提供的iframe代码将日历嵌入到我的网站中。
我遇到的问题是服务器希望用户像我一样登录才能向日历添加事件。由于服务器是添加事件的服务器,因此我不确定如何解决此问题。我知道我需要创建/使用服务帐户,以便我的服务器可以“委派整个域的权限”,但是我不确定如何使用PHP的客户端库执行此操作。谷歌没有提供示例。我必须下载服务器需要进行这些api调用的p12文件,但是我不确定如何使用客户端库指向该文件。是否可以使用php客户端库连接到服务帐户以进行这些api调用?如果是这样,怎么办?
任何帮助是极大的赞赏。
我遇到了同样的问题,并在这里找到了解决方法(以一种大概的方式):使用服务帐户插入Google日历条目
第一件事情是设置你的谷歌日历正确(见上文描述非常好上述职位),然后从这里下载API代码日历https://github.com/google/google-api-php-client这是该页面右侧的“下载ZIP”链接。
然后这是一些对我有用的示例代码(私钥在适当的地方用xxxx代替,但实际上正是我在使用的)。
我现在正试图找出如何阅读和清除Google日历的方法,事实证明这更具挑战性!
<?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 ([email protected] = the calendar owner)
//
calendarize('Test Appointment','Test appt description',$startdate,$enddate,'[email protected]');
//-----------------------------------------------//
// 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 = '[email protected]';
$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();
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句