8

I'm trying to execute a simple freebusy query on the Google Calendar API. I have my authorization all working fine, can see lists of users' calendars, etc. When I try to submit a free/busy query, I get a 400 error with (as far as I can tell) not details at all about what it doesn't like.

This is true both in my own code and when I use the Google Calendar API "Try It" feature.

Here is my request (created using the Try It widget), and the response:

Request

POST https://www.googleapis.com/calendar/v3/freeBusy?key={YOUR_API_KEY}

Content-Type:  application/json
Authorization:  Bearer ya29.AHES6ZTr-2wksEvp0iGPTnHTirTIItib5WwxmSAySq1ghfI98Cz88kA
X-JavaScript-User-Agent:  Google APIs Explorer

{
     "items": [
          {
              "id": "[my calendar id goes here]@group.calendar.google.com"
          }
     ],
     "timeMin": "2012-01-31T09:00:00",
     "timeMax": "2012-01-31T10:00:00",
     "timeZone": "GMT"
}

Response

400 Bad Request

cache-control:  private, max-age=0
content-encoding:  gzip
content-length:  122
content-type:  application/json; charset=UTF-8
date:  Sun, 16 Jun 2013 13:08:32 GMT
expires:  Sun, 16 Jun 2013 13:08:32 GMT
server:  GSE

{
     "error": {
     "errors": [
          {
              "domain": "global",
              "reason": "badRequest",
              "message": "Bad Request"
           }
     ],
    "code": 400,
    "message": "Bad Request"
     }
}

I think it might have something to do with the dateTime objects/formatting? But I don't have any idea what. I'd really appreciate some help; I've been banging my head against this for a week!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
nilatti
  • 528
  • 1
  • 4
  • 14
  • does using `"timeZone": "UTC"` or removing that attribute change anything? Is the API key in the URL being escaped properly? see: http://stackoverflow.com/questions/15926676/google-calendar-api-bad-request-400-event-over-developer-console#comment22712451_15937488 – John Sheehan Jun 17 '13 at 05:59
  • I've tried using UTC, removing the property, and including the timezone offset in the min/max times. None of that has changed anything. Since I'm using Google's API explorer, there's no way to escape anything. – nilatti Jun 18 '13 at 01:26

6 Answers6

28

Ok, I figured it out and I am leaving the answer for anyone else who is LOSING THEIR MIND over this.

Despite what Google's documentation says about format for datetime in this case, the only format I could get to work was this:

2008-03-07T17:06:02.000Z

so that's YYYY-MM-DDTHH:MM:SS.MMMZ

What is the Z for? I don't know, but it has to be there.

nilatti
  • 528
  • 1
  • 4
  • 14
  • Z is for "Zulu"? What. The. Hell. But you can specify time zone in a separate property, and that works. – nilatti Jun 21 '13 at 11:06
  • 3
    Z is a shorthand for the UTC ([Coordinated Universal Time](http://en.wikipedia.org/wiki/Coordinated_Universal_Time)) timezone. Points for your persistence. – Bobulous Oct 03 '13 at 20:16
  • I suspected this as being very specific and you have confirmed it! Thanks – Jared Eitnier Feb 04 '14 at 05:25
  • Thanks for this, bad docs... in PHP you can do date('c') for that format. – Jim Nov 15 '14 at 04:32
  • That format is defined by the [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) standard. In Java, call `Instant.now().toString()` to generate the string and call `Instant.parse( … )` to parse such a string. – Basil Bourque Nov 30 '16 at 08:15
  • Thank you for posting this answer to document the resolution of the problem. Here it is many years later and I encountered the same issue and never would have figured out it was the date format. – infinitecardinal Aug 07 '20 at 17:10
1

I had the same problem, but I managed to solve it simply by removing the "Z" from DateTime and in TimeZone putting GMT with the time difference:

'dateTime': '2020-10-31T17:00:00.000',
'timeZone': 'GMT-03:00'
1

In case someone is facing similar error while using Google APIs Client Library for Objective-C for REST, use dateTime property instead of the date property when setting the date for the event. I wasted several hours on this.

Skywalker
  • 1,590
  • 1
  • 18
  • 36
0

The date time value should be in UTC "TZ" format. Almost all CRMs and public API accept time in this format. If you convert the datetime to UTC time using major language (such as C or Java) library functions/methods they would return a datetime instance in YYYY-MM-DDTHH:MM:SS.MMMZ format.

ntulsi
  • 76
  • 1
  • 13
  • Actually that format is defined by the [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) standard. The `Z` on the end is short for `Zulu` and means UTC. – Basil Bourque Nov 30 '16 at 08:11
0

In short, you are missing the time zone offset in both the timeMin and timeMax variables, this is required even if you have the timeZone variable set.

For example, I live in the America/New_York timezone so my offset would be -05:00, which needs to be appended to the values you currently have. So for me the values would look like,

{
    ...
    "timeMin": "2012-01-31T09:00:00-05:00",
    "timeMax": "2012-01-31T10:00:00-05:00",
    ...
}

To be more specific, Google Calendar API requires that timeMin and timeMax

Must be an RFC3339 timestamp with mandatory time zone offset, for example, 2011-06-03T10:00:00-07:00, 2011-06-03T10:00:00Z. Milliseconds may be provided but are ignored. If timeMax is set, timeMin must be smaller than timeMax.

So you can either have 2012-01-31T09:00:00-5:00 or 2012-01-31T09:00:00Z with the milliseconds as optional (since its ignored anyway).

The Google Calendar Events list API documentation is where I got all the info from.

Klynton
  • 151
  • 7
0

Was something slightly different for me, I was missing the milliseconds:

2023-08-06T07:55:23.000Z

Instead of

2023-08-06T07:55:23Z  
Oded Ben Dov
  • 9,936
  • 6
  • 38
  • 53