0

My PHP site creates calendar events for my colleagues. After I get the request to login to my organization (which administers the calendar), the API successfully retrieves the code. However, the first attempt at a redirect results in the following error:

Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "badRequest", "message": "Bad Request" } ], "code": 400, "message": "Bad Request" } } in /home/{user}/vendor/google/apiclient/src/Google/Http/REST.php:123 Stack trace: #0 /home/{user}/vendor/google/apiclient/src/Google/Http/REST.php(98): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 /home/{user}n/vendor/google/apiclient/src/Google/Task/Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /home/{user}/vendor/google/apiclient/src/Google/Http/REST.php(61): Google_Task_Runner->run() #3 /home/{user}/vendor/google/apiclient/src/Google/Client.php(866): Google_Http_REST::execute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...', Array, NULL) #4 /home/{user}/vendor/google/apiclient/src/Google/Service/Resource.php(232): Googl in /home/{user}/vendor/google/apiclient/src/Google/Http/REST.php on line 123.

If I press the 'back' button on my browser, and resubmit, the request goes through and creates the event. Below is the code that I am using:

$client = new Google_Client();
$client->setAuthConfig("client_credentials.json");
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/mtgset.php');
$client->addScope("https://www.googleapis.com/auth/calendar");

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {

    $client->setAccessToken($_SESSION['access_token']);

    $cal = new Google_Service_Calendar($client);

$event = new Google_Service_Calendar_Event(array(
  'summary' => $summary,
  'location' => 'Online',
  'description' => $phone,
  'start' => array(
    'dateTime' => $start,

  ),
  'end' => array(
    'dateTime' => $end,

  ),

  'attendees' => array(
    array('email' => $parent),
  ),
  'reminders' => array(
    'useDefault' => FALSE,
    'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60),
      array('method' => 'popup', 'minutes' => 10),
    ),
  ),
));


    $conference = new \Google_Service_Calendar_ConferenceData();
    $conferenceRequest = new \Google_Service_Calendar_CreateConferenceRequest();
    $conference->setCreateRequest($conferenceRequest);
    $conferenceRequest->setRequestId('dgresvb');
    $event->setConferenceData($conference);

    $calendarId = 'primary';
    $event = $cal->events->insert($calendarId, $event, ['conferenceDataVersion' => 1]);


} else {

    if (!isset($_GET['code'])) {

          $auth_url = $client->createAuthUrl();
          header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

    } else {

      $client->authenticate($_GET['code']);
      $_SESSION['access_token'] = $client->getAccessToken();

      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/mtgset.php';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

    }

}

I'm guessing that the issue has to do with the fact that OAuth2 has to go through my organization to get authentication, but I don't know how to fix it. Do I need to add another scope?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449

1 Answers1

0

I just figured it out. I had passed an id value from a previous page in order to get information to populate the event. The $_GET value was working if authentication was not required, but the variable was lost if the page had to go to the auth server. Therefore, the event was not being populated properly, which threw the error. I had to create session variables for all of the event data so that the event would always populate properly. Thanks for checking it out.