2

I'm trying to insert an event with Google Calendar API PHP. I've got no error, i've got the $event->hmtlLink, but when I go on, Google say me "This event doesn't exist".

Here is my code to get client and service:

putenv("GOOGLE_APPLICATION_CREDENTIALS=" . __DIR__ . "/private/credentials.json");

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(SCOPES);

$service = new Google_Service_Calendar($client);

$calendarId = "primary";

Here is the function I use to create an event:

function createEvent($eventId, $name, $equipe, $location, $description, $startTimeDate, $endTimeDate, $timeZone){
    $startTimeDate = new DateTime($startTimeDate);
    $startTimeDate = date_format($startTimeDate, DATE_ISO8601);
    $endTimeDate = new DateTime($endTimeDate);
    $endTimeDate = date_format($endTimeDate, DATE_ISO8601);
    $event = new Google_Service_Calendar_Event(array(
      'id' => $eventId,
      'summary' => join('',array("[",$equipe,"] ",$name)),
      'location' => $location,
      'description' => $description,
      'start' => array(
        'dateTime' => $startTimeDate,
        'timeZone' => $timeZone,
      ),
      'end' => array(
        'dateTime' => $endTimeDate,
        'timeZone' => $timeZone,
      ),
    ));
    global $service, $calendarId;
    $event = $service->events->insert($calendarId, $event);
    printf($event->htmlLink);
    printf($event->status);
    printf($event->created);
}

The 1st printf send me a good link, but on opening: event not existing

The 2nd send "confirmed"

The 3rd send the good dateTime

When I try to insert an event with the same id, I've got an error saying this id already exists. But nothing in the calendar...

Help me please ! Thank you.

  • Just a quick guess here, but confirm that you're using the same account that you're creating the event with (in your credentials file) to view the html link - if you have another Google account it's possible it is coming up as default and you don't have the correct permissions to see the event that was added. – James Alday May 16 '17 at 19:29
  • I never developed anything with another account, and in my credientials, the `client_email` is the good, so I think it's the good account. – Thomas Caillier May 17 '17 at 07:21
  • If the credentials you're adding the event under are for your email address, try manually logging into calendar with those credentials and see if you can see that calendar and any events attached to it without using the direct link. Also, check this answer for how to get a direct link from the API. You can use it to confirm your htmlLink is returning the same value: http://stackoverflow.com/a/23469684/463935 – James Alday May 17 '17 at 13:50

1 Answers1

0

You didn't posted here the full get-events request, but there might be some filters in your request which filters out some events, so when you send the request it won't include all events.

For example there's a DateTime parameter called TimeMin, which filters out all events prior to that DateTime, and for example if you set this to DateTime.Now.AddYears(1), then you won't get access to any events scheduled prior to a year from now.

Mayer Spitz
  • 2,577
  • 1
  • 20
  • 26