使用php将事件添加到Google日历 [英] adding an event to google calendar using php

查看:126
本文介绍了使用php将事件添加到Google日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个客户端Web应用程序,用户可以在其中预订带有日期,时间,位置等信息的驱动器

I'm working on a client web app where users can book drives with date, time, location...etc

客户要求将每次预订都添加为他的Google日历中的事件

the client required that every booking is added as an event on his google calendar

我创建了一个API密钥并下载了PHP API客户端: https://github.com/google/google-api-php-client

I created an API key and downloaded the PHP API client: https://github.com/google/google-api-php-client

但是当我尝试添加事件时,出现需要登录"错误

But when I try to add an event I get a "Login Required" Error

我如何直接添加事件而无需使用OAuth和同意屏幕,因为该功能将在后端自动执行,因为我拥有对gmail帐户的完全访问权限.

How can I add an event directly without having to use OAuth and consent screen because the function will be executed automatically in the backend, I have full access to the gmail account.

推荐答案

我使用服务帐户并根据此

I got it to work using a service account and some steps based on this answer, here is what I did:

1-在 Google开发者控制台上创建一个项目.

1- Create a project on Google Developer Console.

2-转到凭据并创建一个密钥类型为JSON的服务帐户密钥,将下载一个JSON文件,并将其移动到您的项目文件夹中.

2- Go to Credentials and create a Service account key with key type JSON, a JSON file will be downloaded, move it to your project folder.

3-从库"标签启用日历API,搜索日历API并启用它.

3- Enable Calendar API from Library tab, search for Calendar API and enable it.

4-转到您的 Google日历

5-转到设置"->添加日历"->新日历",然后弹出通知/吐司,单击配置",向下滚动到与特定人员共享"->添加人员",在电子邮件字段上添加服务帐户ID ,您可以从凭据"->管理服务帐户"中获取它,然后设置权限"以对事件进行更改,然后单击发送".

5- Go to Settings -> Add calendar -> New calendar, after that a notification/toast will popup click on Configure, scroll down to Share with specific people -> Add people, on the email field add the service account ID, you can get it from Credentials -> Manage service accounts then set the Permission to Make changes to events and click Send.

6-下载 PHP客户端库.

7-现在,您需要获取日历ID,从日历设置向下滚动到最后一部分,您将找到它,或者这里是一个示例代码,可以在响应中查找它,就像这样'j85tnbuj1e5tgnizqt9faf2i88@group.calendar.google.com':

7- Now you need to get the calendar ID, from calendar settings scroll down to last section you will find it, or here is a sample code to get it, look for it in the response, it will be something like this 'j85tnbuj1e5tgnizqt9faf2i88@group.calendar.google.com':

<?php
require_once 'google-api/vendor/autoload.php';

$client = new Google_Client();
//The json file you got after creating the service account
putenv('GOOGLE_APPLICATION_CREDENTIALS=google-api/test-calendar-serivce-1ta558q3xvg0.json');
$client->useApplicationDefaultCredentials();
$client->setApplicationName("test_calendar");
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');

$service = new Google_Service_Calendar($client);

$calendarList = $service->calendarList->listCalendarList();
print_r($calendarList);
?>

8-您现在可以将事件添加到日历中,示例代码:

8- You can now add events to the calendar, sample code:

$event = new Google_Service_Calendar_Event(array(
  'summary' => 'Test Event',
  'description' => 'Test Event',
  'start' => array(
    'dateTime' => '2018-06-02T09:00:00-07:00'
  ),
  'end' => array(
    'dateTime' => '2018-06-10T09:00:00-07:00'
  )
));

$calendarId = 'j85tnbuj1e5tgnizqt9faf2i88@group.calendar.google.com';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %s\n', $event->htmlLink);

这里发生的是,该事件是由服务帐户创建的,该事件与您自己的Google帐户不同,并且拥有自己的数据,因此,如果您不与服务帐户共享日历,则将日历ID设置为主将在您通常无法访问的服务帐户日历上创建活动.

What happens here is that the event is created by the service account which is different than your own google account and has it's own data so if you didn't share the calendar with the service account and set the calendar id to primary it will create the event on the service account calendar which you can't access it normally.

我希望这对任何人都有帮助.

I hope this helps anyone.

参考文献:
https://stackoverflow.com/a/26067547/8702128
https://github.com/google/google-api-php-client
https://developers.google.com/calendar/quickstart/php
如何使用php将事件插入用户google日历?

References:
https://stackoverflow.com/a/26067547/8702128
https://github.com/google/google-api-php-client
https://developers.google.com/calendar/quickstart/php
How to insert event to user google calendar using php?

这篇关于使用php将事件添加到Google日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆