Javascript Google日历API未经授权插入事件

Javascript Google日历API未经授权插入事件,javascript,php,api,google-calendar-api,Javascript,Php,Api,Google Calendar Api,我需要授权我网站上的用户在我们的谷歌日历中插入事件。这是一个预订系统,用户在其中选择服务、确认,并自动在我们的日历中插入新事件。 问题是,我在客户端页面上有一个我不需要的授权按钮 <button id="authorize-button" onclick="handleAuthClick(event)">Authorize</button> 另外,该站点位于php上。是的,我需要在php sdk中使用服务帐户。您下载p12密钥,在服务器上上载,然后: require_o

我需要授权我网站上的用户在我们的谷歌日历中插入事件。这是一个预订系统,用户在其中选择服务、确认,并自动在我们的日历中插入新事件。 问题是,我在客户端页面上有一个我不需要的授权按钮

<button id="authorize-button" onclick="handleAuthClick(event)">Authorize</button>

另外,该站点位于php上。

是的,我需要在php sdk中使用服务帐户。您下载p12密钥,在服务器上上载,然后:

require_once ('../src/Google/autoload.php');

$client_id = '{your_code}.apps.googleusercontent.com'; //Client ID
$service_account_name = '{your_email}@developer.gserviceaccount.com'; //Email Address
$key_file_location = $_SERVER['DOCUMENT_ROOT'] .'{file}.p12'; //key.p12
$client = new Google_Client(); //AUTHORIZE OBJECTS
$client->setApplicationName("App Name");

//INSTATIATE NEEDED OBJECTS (In this case, for freeBusy query, and Create New Event)
$service = new Google_Service_Calendar($client);
$id = new Google_Service_Calendar_FreeBusyRequestItem($client);
$item = new Google_Service_Calendar_FreeBusyRequest($client);
$event = new Google_Service_Calendar_Event($client);
$startT = new Google_Service_Calendar_EventDateTime($client);
$endT = new Google_Service_Calendar_EventDateTime($client);


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()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();

/************************************************
  MAIN FUNCTIONS
 ************************************************/

function GetFreeBusy($calendar_id, $calendar_date) {
  global $id; //GET OBJECTS FROM OUTSIDE
  global $item;
  global $service;
  $arrayTime = array();
  $id->setId($calendar_id);
  $item->setItems(array($id));
  $item->setTimeZone('Europe/Rome');
  $item->setTimeMax("{$calendar_date}T18:00:00+02:00");
  $item->setTimeMin("{$calendar_date}T08:00:00+02:00");
  $query = $service->freebusy->query($item);

  $start = $query["calendars"][$calendar_id]["busy"];
  $end = $query["calendars"][$calendar_id]["busy"];

  $length = count($start);
  for ($i = 0; $i < $length; $i++) {
    $startTime = $start[$i]["start"];
    $endTime = $start[$i]["end"];

    list($a, $b) = explode('T', $startTime);
    list($startHour, $d) = explode(':00+', $b);
    list($e, $f) = explode('T', $endTime);
    list($endHour, $g) = explode(':00+', $f);
    array_push($arrayTime, array($startHour, $endHour));
    // I CREATED AN ARRAY FOR MY NEEDS ex. [ ["8:00", "10:00"], ["14:00", "14:30"] ]
  }
  return $arrayTime;
}

function CreateEvent($calendarId, $summary, $location, $description, $date, $start, $end) {

  global $service;
  global $event;
  global $startT;
  global $endT;

  $startT->setTimeZone("Europe/Rome");
  $startT->setDateTime($date."T".$start.":00");
  $endT->setTimeZone("Europe/Rome");
  $endT->setDateTime($date."T".$end.":00");

  $event->setSummary($summary);
  $event->setLocation($location);
  $event->setDescription($description);
  $event->setStart($startT);
  $event->setEnd($endT);

if($insert = $service->events->insert($calendarId, $event)) {
  return true;
}

}
require_once('../src/Google/autoload.php');
$client_id='{your_code}.apps.googleusercontent.com'//客户端ID
$service_account_name='{your_email}@developer.gserviceaccount.com'//电子邮件地址
$key_file_location=$_服务器['DOCUMENT_ROOT'].{file}.p12'//图例1.p12
$client=新的Google_客户端()//授权对象
$client->setApplicationName(“应用程序名称”);
//安装所需的对象(在本例中,用于忙/闲查询,并创建新事件)
$service=新谷歌服务日历($client);
$id=新的Google\u服务\u日历\u FreeBusyRequestItem($client);
$item=newgoogle\u服务\u日历\u FreeBusyRequest($client);
$event=新谷歌服务\日历\事件($client);
$startT=新谷歌服务\日历\事件日期时间($client);
$endT=新谷歌服务\日历\事件日期时间($client);
如果(isset($_会话['service_token'])){
$client->setAccessToken($_会话['service_token']);
}
$key=file\u get\u contents($key\u file\u location);
$cred=新的谷歌认证断言(
$service\u account\u name,
数组('https://www.googleapis.com/auth/calendar'),
$key
);
$client->setAssertionCredentials($cred);
如果($client->getAuth()->isAccessTokenExpired()){
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$\会话['service\u token']=$client->getAccessToken();
/************************************************
主要功能
************************************************/
函数GetFreeBusy($calendar\u id,$calendar\u date){
全局$id;//从外部获取对象
全球$项目;
全球$服务;
$arrayTime=array();
$id->setId($calendar\u id);
$item->setItems(数组($id));
$item->setTimeZone('Europe/Rome');
$item->setTimeMax(“{$calendar\u date}T18:00:00+02:00”);
$item->setTimeMin(“{$calendar\u date}T08:00:00+02:00”);
$query=$service->freebusy->query($item);
$start=$query[“日历”][$calendar\u id][“忙碌”];
$end=$query[“日历”][$calendar\u id][“忙碌”];
$length=计数($start);
对于($i=0;$i<$length;$i++){
$startTime=$start[$i][“start”];
$endTime=$start[$i][“end”];
列表($a,$b)=爆炸($T',$startTime);
列表($startHour,$d)=爆炸(':00+',$b);
列表($e,$f)=爆炸($T',$endTime);
列表($endHour,$g)=分解(':00+',$f);
array_push($arrayTime,array($startHour,$endHour));
//我根据自己的需要创建了一个数组,例如:[“8:00”、“10:00”]、[“14:00”、“14:30”]]
}
返回$arrayTime;
}
函数CreateEvent($calendarId、$summary、$location、$description、$date、$start、$end){
全球$服务;
全球$活动;
全球$startT;
全球$endT;
$startT->setTimeZone(“欧洲/罗马”);
$startT->setDateTime($date.T.“$start.”:00”);
$endT->setTimeZone(“欧洲/罗马”);
$endT->setDateTime($date.T.“$end.”:00”);
$event->setSummary($summary);
$event->setLocation($location);
$event->setDescription($description);
$event->setStart($startT);
$event->setEnd($endT);
如果($insert=$service->events->insert($calendarId,$event)){
返回true;
}
}

您可以打开日历,添加并与{your_email}@developer.gserviceaccount.com共享。现在,任何用户都可以为我的cal创建事件。

是的,我需要在php sdk中使用服务帐户。您下载p12密钥,在服务器上上载,然后:

require_once ('../src/Google/autoload.php');

$client_id = '{your_code}.apps.googleusercontent.com'; //Client ID
$service_account_name = '{your_email}@developer.gserviceaccount.com'; //Email Address
$key_file_location = $_SERVER['DOCUMENT_ROOT'] .'{file}.p12'; //key.p12
$client = new Google_Client(); //AUTHORIZE OBJECTS
$client->setApplicationName("App Name");

//INSTATIATE NEEDED OBJECTS (In this case, for freeBusy query, and Create New Event)
$service = new Google_Service_Calendar($client);
$id = new Google_Service_Calendar_FreeBusyRequestItem($client);
$item = new Google_Service_Calendar_FreeBusyRequest($client);
$event = new Google_Service_Calendar_Event($client);
$startT = new Google_Service_Calendar_EventDateTime($client);
$endT = new Google_Service_Calendar_EventDateTime($client);


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()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();

/************************************************
  MAIN FUNCTIONS
 ************************************************/

function GetFreeBusy($calendar_id, $calendar_date) {
  global $id; //GET OBJECTS FROM OUTSIDE
  global $item;
  global $service;
  $arrayTime = array();
  $id->setId($calendar_id);
  $item->setItems(array($id));
  $item->setTimeZone('Europe/Rome');
  $item->setTimeMax("{$calendar_date}T18:00:00+02:00");
  $item->setTimeMin("{$calendar_date}T08:00:00+02:00");
  $query = $service->freebusy->query($item);

  $start = $query["calendars"][$calendar_id]["busy"];
  $end = $query["calendars"][$calendar_id]["busy"];

  $length = count($start);
  for ($i = 0; $i < $length; $i++) {
    $startTime = $start[$i]["start"];
    $endTime = $start[$i]["end"];

    list($a, $b) = explode('T', $startTime);
    list($startHour, $d) = explode(':00+', $b);
    list($e, $f) = explode('T', $endTime);
    list($endHour, $g) = explode(':00+', $f);
    array_push($arrayTime, array($startHour, $endHour));
    // I CREATED AN ARRAY FOR MY NEEDS ex. [ ["8:00", "10:00"], ["14:00", "14:30"] ]
  }
  return $arrayTime;
}

function CreateEvent($calendarId, $summary, $location, $description, $date, $start, $end) {

  global $service;
  global $event;
  global $startT;
  global $endT;

  $startT->setTimeZone("Europe/Rome");
  $startT->setDateTime($date."T".$start.":00");
  $endT->setTimeZone("Europe/Rome");
  $endT->setDateTime($date."T".$end.":00");

  $event->setSummary($summary);
  $event->setLocation($location);
  $event->setDescription($description);
  $event->setStart($startT);
  $event->setEnd($endT);

if($insert = $service->events->insert($calendarId, $event)) {
  return true;
}

}
require_once('../src/Google/autoload.php');
$client_id='{your_code}.apps.googleusercontent.com'//客户端ID
$service_account_name='{your_email}@developer.gserviceaccount.com'//电子邮件地址
$key_file_location=$_服务器['DOCUMENT_ROOT'].{file}.p12'//图例1.p12
$client=新的Google_客户端()//授权对象
$client->setApplicationName(“应用程序名称”);
//安装所需的对象(在本例中,用于忙/闲查询,并创建新事件)
$service=新谷歌服务日历($client);
$id=新的Google\u服务\u日历\u FreeBusyRequestItem($client);
$item=newgoogle\u服务\u日历\u FreeBusyRequest($client);
$event=新谷歌服务\日历\事件($client);
$startT=新谷歌服务\日历\事件日期时间($client);
$endT=新谷歌服务\日历\事件日期时间($client);
如果(isset($_会话['service_token'])){
$client->setAccessToken($_会话['service_token']);
}
$key=file\u get\u contents($key\u file\u location);
$cred=新的谷歌认证断言(
$service\u account\u name,
数组('https://www.googleapis.com/auth/calendar'),
$key
);
$client->setAssertionCredentials($cred);
如果($client->getAuth()->isAccessTokenExpired()){
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$\会话['service\u token']=$client->getAccessToken();
/************************************************
主要功能
************************************************/
函数GetFreeBusy($calendar\u id,$calendar\u date){
全局$id;//从外部获取对象
全球$项目;
全球$服务;
$arrayTime=array();
$id->setId($calendar\u id);
$item->setItems(数组($id));
$item->setTimeZone('Europe/Rome');
$item->setTimeMax(“{$calendar\u date}T18:00:00+02:00”);
$item->setTimeMin(“{$calendar\u date}T08:00:00+02:00”);
$query=$service->freebusy->query($item);
$start=$query[“日历”][$calendar\u id][“忙碌”];
$end=$query[“日历”][$calendar\u id][“忙碌”];
$length=计数($start);
对于($i=0;$i<$length;$i++){
$startTime=$start[$i][“start”];
$endTime=$start[$i][“end”];
列表($a,$b)=爆炸($T',$startTime);
列表($startHour,$d)=爆炸(':00+',$b);
列表($e,$f)=爆炸($T',$endTime);
列表($endHour,$g)=分解(':00+',$f);
阵列推送($arrayTime,阵列($startHou)