使用php无法正常将事件插入谷歌日历

时间:2017-04-15 12:18:38

标签: php google-calendar-api

我正在使用谷歌api将事件插入谷歌日历,但有一个问题。当我运行它时返回一个错误:

  

404   错误:invalid_request

     

缺少必需参数:范围

     

了解详情

     

请求详细信息

     

RESPONSE_TYPE =代码

     

ACCESS_TYPE =在线

     

CLIENT_ID = #####。com

     

REDIRECT_URI = http://index.php

     

状态=

     

范围=

     

approval_prompt =自动

这是我的代码,请检查并让我知道我做错了什么

require_once __DIR__ . '/google-api-php-client-master/src/Google/vendor/autoload.php';
require_once __DIR__ . "/google-api-php-client-master/src/Google/Client.php";
error_reporting(E_ALL);
//require_once 'google-api-php-client/src/Google_Client.php';
require_once __DIR__ . '/google-api-php-client-master/src/Google/Calendar.php';
session_start();

if ((isset($_SESSION)) && (!empty($_SESSION))) {
   echo "There are cookies<br>";
   echo "<pre>";
   print_r($_SESSION);
   echo "</pre>";
}
$scopes ="https://www.googleapis.com/auth/calendar.readonly";
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
$client->setClientId('####.com');
$client->setClientSecret(FG#####');
$client->setRedirectUri('http://index.php');
$client->setDeveloperKey('h#####');
$cal = new Google_Service_Calendar_Calendar($client);

if (isset($_GET['logout'])) {
  echo "<br><br><font size=+2>Logging out</font>";
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  echo "<br>I got a code from Google = ".$_GET['code']; // You won't see this if redirected later
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
  echo "<br>I got the token = ".$_SESSION['token']; // <-- not needed to get here unless location uncommented
}

if (isset($_SESSION['token'])) {
  echo "<br>Getting access";
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()){

  echo "<hr><font size=+1>I have access to your calendar</font>";
  $event = new Google_Event();
  $event->setSummary('Halloween');
  $event->setLocation('The Neighbourhood');
  $start = new Google_EventDateTime();
  $start->setDateTime('2013-9-29T10:00:00.000-05:00');
  $event->setStart($start);
  $end = new Google_EventDateTime();
  $end->setDateTime('2013-9-29T10:25:00.000-05:00');
  $event->setEnd($end);
  $createdEvent = $cal->events->insert('###', $event);
  echo "<br><font size=+1>Event created</font>";

  echo "<hr><br><font size=+1>Already connected</font> (No need to login)";

} else {

  $authUrl = $client->createAuthUrl();
  //echo "<pre>"; print_r($authUrl); die;
  print "<hr><br><font size=+2><a href='$authUrl'>Connect Me!</a></font>";

}

$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
echo "<br><br><font size=+2><a href=$url?logout>Logout</a></font>";

0 个答案:

没有答案