0

I am using google api to insert event into google calendar but there is a problem. when i run it return me an error:

404 Error: invalid_request

Missing required parameter: scope

Learn more

Request Details

response_type=code

access_type=online

client_id=#####.com

redirect_uri=http://index.php

state=

scope=

approval_prompt=auto

Here is my code please check and let me know what i am doing wrong

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>";
Simon H
  • 2,495
  • 4
  • 30
  • 38
  • Possible duplicate of [How to insert event to user google calendar using php?](http://stackoverflow.com/questions/34541009/how-to-insert-event-to-user-google-calendar-using-php) – RïshïKêsh Kümar Apr 15 '17 at 12:21
  • http://stackoverflow.com/questions/34541009/how-to-insert-event-to-user-google-calendar-using-php --- Look here – RïshïKêsh Kümar Apr 15 '17 at 12:21
  • is this actually your redirect url? If so that's your problem. `http://index.php` It needs to point to a real publicly accessible website. – Jonathan Apr 15 '17 at 13:12
  • I tried everything but its not working now i am facing :Fatal error: Uncaught Error: Call to a member function insert() – susheel jamwal Apr 17 '17 at 10:06

0 Answers0