我知道这个事实,我不是第一个问这个问题,但我一直在寻找几天,现在似乎无法找到解决方案问题。 因此,事情是我需要用Google帐户设置我的应用程序,并且每当用户完成一项操作时,应用程序就会向应用程序的Google日历中添加一个事件,而不会打扰用户的任何同意。我需要在代码中完成应用程序帐户的所有身份验证。我已经做了一些搜索,我认为通过服务帐户进行Google Server到服务器通信是一种方式。 https://developers.google.com/accounts/docs/OAuth2ServiceAccount
谷歌日历V3服务帐户在ZF2中的使用
事情是我不知道如何设置客户端,获取访问令牌并与它调用API。到目前为止,我已经使用作曲家安装谷歌API:
{
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.3.*",
"dlu/dlutwbootstrap": "dev-master",
"google/apiclient": "1.0.*@beta"
}
}
而且我还创建了一个服务帐户由谷歌开发者控制台,并获得客户端ID,电子邮件等。因此,这给我们带来了代码:
$client = new \Google_Client();
$clientId = '<MY ID>';
$service_email = '<THE EMAIL>';
$myMail = '<MY OTHER MAIL>';
$keyFile = './data/<NAME OF FILE>';
$client->setClientId($clientId);
$client->setApplicationName('Timesheet');
$key = file_get_contents($keyFile);
$client->setAssertionCredentials(new Google_AssertionCredentials(
$service_email,
array('https://www.googleapis.com/auth/calendar'),
$key));
$client->getRequestToken();
$calendar = new \Google_Service_Calendar($client);
$event = new Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new EventDateTime();
$start->setDateTime('2014-11-05T10:00:00.000-07:00');
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime('2014-11-05T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new EventAttendee();
$attendee1->setEmail('<EMAIL ONE>');
$attendee2 = new EventAttendee();
$attendee2->setEmail('<EMAIL TWO>');
$attendees = array($attendee1, $attendee2);
$event->attendees = $attendees;
$createdEvent = $calendar->events->insert('<CALENDAR TO INSERT EVENT INTO>', $event);
我真的很感激,如果有人能指出我真的错过了什么,并引导我进入正确使用API。先谢谢你!
最好的问候,
瓦伦丁
我不认为我理解这个问题。服务帐户是一个帐户。如果您只是希望用户将事件添加到服务帐户日历中,则可以使用它来执行此操作。如果您想要将事件添加到用户日历,则首先需要他们的许可。 “无论什么时候完成”< - 您的意思是一个 – DaImTo 2014-11-06 08:42:57
我的意思是每当一个用户完成一个动作时。因此,可以说用户有以下邮件:[email protected],他有他自己的日历(我们不感兴趣)。该应用程序本身通过电子邮件进行身份验证:[email protected]并具有Google日历。我想将事件添加到[email protected]的日历中,因此不需要用户同意。 – 2014-11-06 09:16:49