【问题标题】:Google Calendar API Insert event without authorizationGoogle Calendar API 未经授权插入事件
【发布时间】:2015-07-15 02:23:40
【问题描述】:

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

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

所以我想我需要一个服务帐户授权。但我不知道如何在 javascript 中使用生成的 json 和 p12 键。 我需要一个样本,或者至少要了解我必须做什么..

JS代码:

var CLIENT_ID = '*********.apps.googleusercontent.com';
var API_KEY = 'AIza***********************';
var SCOPES = ["https://www.googleapis.com/auth/calendar"];

function handleClientLoad() {
  gapi.client.setApiKey(API_KEY);
  window.setTimeout(checkAuth, 1);
}

function checkAuth() {
  gapi.auth.authorize({
    client_id: CLIENT_ID,
    scope: SCOPES,
    immediate: true
  }, handleAuthResult);
}

function handleAuthResult(authResult) {
  var authorizeButton = document.getElementById('authorize-button');
  if (authResult && !authResult.error) {
    authorizeButton.style.visibility = 'hidden';
  } else {
    authorizeButton.style.visibility = '';
    authorizeButton.onclick = handleAuthClick;
  }
}

function handleAuthClick(event) {
  gapi.auth.authorize({
    client_id: CLIENT_ID,
    scope: SCOPES,
    immediate: false
  }, handleAuthResult);
  return false;
}

附: - 网站在 php 上。

【问题讨论】:

    标签: javascript php api google-calendar-api


    【解决方案1】:

    是的,我需要在 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;
    }
    
    }
    

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

    【讨论】:

      猜你喜欢
      • 2014-10-22
      • 2012-11-19
      • 1970-01-01
      • 2018-10-15
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多