googleworkspace / php-samples

PHP samples for Google Workspace APIs
Apache License 2.0
288 stars 347 forks source link

Error in response type OPTIONS method #48

Closed NicolasNUA closed 5 years ago

NicolasNUA commented 5 years ago

Hello, in my project I need to read the events that the user has in Google calendar, what I do is follow the procedure of this web page and when I am requesting the data, it shows an error status 405 method, saying that I must enable the "cors" and I do not know in which part of the api that I have in my google account I can do that, or if I have to initiate a curl in php to enable that. Code:

if (
  isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
  strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'
) {
  $the_html = $_GET['action']();
  echo $the_html;
} else {
  echo "don't be silly";
  die();
}
function get_times()
{
  require_once('./connection.php');
  $date = new DateTime($_GET['date']);
  $day_after = new DateTime($_GET['date']);
  $day_after->add(new DateInterval('P1D'));  
  for($i=0;$i<24;$i++) {
      $innerHTML_arr[] = "<option value='$i'>$i:00</option>";
  }
  $client = getClient();
  $service = new Google_Service_Calendar($client);
  $calendarId = 'primary';
  $optParams = array(
    'timeMin' => date('c',$date->getTimestamp()),
    'timeMax' => date('c',$day_after->getTimestamp()),
  );
  $results = $service->events->listEvents($calendarId, $optParams);
  $the_html="";
  foreach ($results->getItems() as $event) {
      $s = new DateTime($event->start->dateTime);
      $start = date('G',$s->getTimestamp());
      $minutes = date('i', $s->getTimestamp());
      unset($innerHTML_arr[$start][$minutes]);
  }
  $the_html.=implode("",$innerHTML_arr);
  return $the_html;
}

the last code is in a php file that i call from other php file with a request GET, with javascript pure

Code:

function get_times(date_picker) {            
            var date = date_picker.value;
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById('dump').innerHTML = xhttp.responseText;
                }
            };
            xhttp.open('GET', './calendars.php?action=get_times&date=' + date + '&t=' + Math.random());
            xhttp.setRequestHeader('X-Requested-With', 'xmlhttprequest');
            xhttp.send();
        }

Expected Behavior

Select a any date and the browser show: Event1 Event2 Event3 ...

Actual Behavior

  1. That’s an error. The requested URL was not found on this server. That’s all we know.

Specifications

erickoledadevrel commented 5 years ago

It looks like you are using custom code, and not running the quickstart as-is. For Calendar API support options please see the following page:

https://developers.google.com/calendar/support