问题描述
我在使用google api方面的经验很少,而且我发现很难找到某些主题的相关信息。
我正在尝试做什么是创建一个不需要用户登录信息的日历。我希望我的服务器根据数据库中的信息将事件添加到日历中。
我已经进入了我的谷歌日历,创建了一个新日历,并处理了一些代码将事件添加到日历。然后我使用google提供的iframe代码将日历嵌入到我的网站中。
我遇到的问题是服务器希望用户登录作为我将事件添加到日历。由于服务器是添加事件的服务器,因此我不太确定如何解决此问题。 ,但我不确定如何使用PHP的客户端库执行此操作。没有例子是由谷歌提供的。我必须下载一个p12文件,我的服务器需要这些api调用,但我不知道如何使用客户端库指向该文件。是否有可能使用PHP客户端库挂钩到一个服务帐户,使这些API调用?如果是这样,怎么样?
任何帮助都非常感谢。
>我遇到了同样的问题,并在这里找到了解决方案(在一段时间内):首先要正确设置您的Google日历(请参阅上面提及的非常详细的描述),然后从这里下载日历的API代码
这是该页面右侧的下载ZIP链接。
然后这里是一些示例代码适用于我(私有密钥在适当的情况下替换为xxxx,否则就是我正在使用的)。
我现在试图了解如何阅读&清除谷歌日历,这是更具挑战性的!
<?php
//
//从例子构建:
// 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');
//
//调用函数将一个事件添加到日历(xxxxxxxxxxx@googlemail.com =日历所有者)
//
calendarize('Test Appointment','测试appt描述',$ startdate,$ enddate,'xxxxxxxxxxx @ googlemail.com');
// ---------------------------------------- ------- //
//功能将事件添加到我的Google日历//
// ------------------ ----------------------------- //
函数calendarize($ title,$ desc,$ start_ev_datetime,$ end_ev_datetime, $ cal_id){
session_start();
require_once'../google-api-php-client-master/autoload.php';
// Google凭据
$ client_id ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.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(无论您的应用名称是什么);
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;
//设置事件数据
$ 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'事件成功添加ID:'。$ createdEvent-> getId();
}
?>
I have very little experience working with the google api and I'm finding it difficult to find relevant information on certain subjects.
What I'm trying to do is create a calendar that requires no log in information from the user. I want my server to add events to the calendar based off of information in a database.
I have gone into my google calendars, created a new one, and worked up some code to add events to the calendar. Then I use the iframe code that google provides to embed the calendar into my site.
The problem I'm running into is that the server wants the user to be logged in as me to add events to the calendar. Since the server is the one adding the events, I'm not really sure how to work around this. I know I need to make/use a service account so my server can "delegate domain-wide authority", but I'm not sure how to do this using the client library for PHP. No examples are provided by google. I had to download a p12 file that my server needs to make these api calls, but I'm not sure how to point to the file using the client library. Is it possible to use the php client library to hook into a service account to make these api calls? If so, how?
Any help is greatly appreciated.
I had the same problem and found the solution (in a round about way) here:Inserting Google Calendar Entries with Service Account
First thing is to set up your Google calendar correctly (see above mentioned post which describes it very well), then download the API code for the Calendar from here https://github.com/google/google-api-php-clientIt's the Download ZIP link on the right of that page.
Then here is some sample code which works for me (private keys replaced with xxxx where appropriate, but otherwise it's exactly what I'm using).
I'm now trying to find out how to read & clear a Google Calendar, which is proving even more challenging!
<?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的客户端库将事件添加到Google日历中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!