title: Creating a new calendar using the Google Calendar API insert method in Javascript returns error 400 "Missing title"
body: <p>I am trying to create a new calendar in my Google account using Javascript (through the Google Apps Script editor). </p>
  
  <p>First, to make sure my Oauth2 is working with the Calendar API, I make a <a href="https://developers.google.com/google-apps/calendar/v3/reference/calendars/get" rel="nofollow noreferrer">GET request</a> to get my primary calendar: </p>
  
  <pre><code>var email = 'me@mydomain.com';
  
  var fetchArgs = {};
  fetchArgs.headers = {'Authorization': 'Bearer '+ calendarOauth2Service.getAccessToken() };
  fetchArgs.method = 'GET';
  fetchArgs.muteHttpExceptions = true;
  
  var url = 'https://www.googleapis.com/calendar/v3/calendars/' + email;
  
  var response = UrlFetchApp.fetch(url, fetchArgs);
  </code></pre>
  
  <p>The request is successful and returns this response: </p>
  
  <pre><code>{
   "kind": "calendar#calendar",
   "etag": "\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"",
   "id": "me@mydomain.com",
   "summary": "me@mydomain.com",
   "timeZone": "America/New_York"
  }
  </code></pre>
  
  <p>Now I want to use the <a href="https://developers.google.com/google-apps/calendar/v3/reference/calendars/insert" rel="nofollow noreferrer">insert method</a> to add a calendar to my account: </p>
  
  <pre><code>var calendarResource = {
         "summary": "test calendar title"
        };
  
  var fetchArgs = {};
  fetchArgs.headers = {'Authorization': 'Bearer '+ calendarOauth2Service.getAccessToken() };
  fetchArgs.method = "POST";
  fetchArgs.data = calendarResource;
  fetchArgs.muteHttpExceptions = true;
  
  var url = 'https://www.googleapis.com/calendar/v3/calendars';
  
  var response = UrlFetchApp.fetch(url, fetchArgs);
  </code></pre>
  
  <p>The calendar is not created, and this is the response:</p>
  
  <pre><code>{
   "error": {
    "errors": [
     {
      "domain": "global",
      "reason": "required",
      "message": "Missing title."
     }
    ],
    "code": 400,
    "message": "Missing title."
   }
  }
  </code></pre>
  
  <p>I've been working on this for almost 20 hours straight now (not an exaggeration) and I can't figure it out. I've tried the following things: </p>
  
  <ol>
  <li>Changing <code>fetchArgs.data = calendarResource;</code> to <code>fetchArgs.body = calendarResource;</code></li>
  <li>Changing <code>fetchArgs.data = calendarResource;</code> to <code>fetchArgs.resource = calendarResource;</code></li>
  <li>Changing <code>fetchArgs.data = calendarResource;</code> to <code>fetchArgs.payload = calendarResource;</code></li>
  <li>Changing <code>fetchArgs.data = calendarResource;</code> to <code>fetchArgs.summary = calendarResource;</code></li>
  <li>Changing <code>fetchArgs.data = calendarResource;</code> to <code>fetchArgs.title = calendarResource;</code></li>
  <li>Moving <code>fetchArgs.data = calendarResource;</code> in to the <code>fetchArgs.header</code> object</li>
  <li>Moving <code>fetchArgs.method = "POST";</code> in to the <code>fetchArgs.header</code> object</li>
  <li>Adding <code>fetchArgs.contentType = 'application/json;</code> to both the <code>fetchArgs</code> object and the <code>fetchArgs.header</code> object</li>
  <li>And many other things, can't think of them all right now</li>
  </ol>
  
  <p>There aren't that many questions related to this on SO. These are the ones I've been trying things from: </p>
  
  <p><a href="https://stackoverflow.com/questions/26876281/google-calendar-rest-api-errors">Google Calendar REST API errors</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9453812/google-calendar-insert-api-returning-400-required">Google Calendar.Insert API returning 400 &#39;required&#39;</a> </p>
  
  <p>Here is <a href="https://stackoverflow.com/questions/32144042/making-google-calendar-api-calls-from-google-apps-script-using-oauth2-and-domain">my original question</a> in which I was trying to do this for another calendar on my Google Apps domain, but since I wasn't able to get that to work, I decided to create this question to remove the domain-wide delegation issue from the equation and just focus on the API call. </p>
  
  <p>Also, one more thing worth mentioning: I am able to successfully create the calendars using the APIs Explorer on the <a href="https://developers.google.com/google-apps/calendar/v3/reference/calendars/insert" rel="nofollow noreferrer">Google Calendar API Calendars: insert</a> documentation page. </p>
  
  <p>It shows me that the request was this: </p>
  
  <pre><code>POST https://www.googleapis.com/calendar/v3/calendars
  {
   "summary": "test calendar title"
  }
  </code></pre>
  
  <p>And the response was this: </p>
  
  <pre><code>200 OK
  - HIDE HEADERS -
  cache-control:  no-cache, no-store, max-age=0, must-revalidate
  content-encoding:  gzip
  content-length:  193
  content-type:  application/json; charset=UTF-8
  date:  Sat, 22 Aug 2015 14:34:05 GMT
  etag:  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  expires:  Fri, 01 Jan 1990 00:00:00 GMT
  pragma:  no-cache
  server:  GSE
  vary:  Origin, X-Origin
  {
  
   "kind": "calendar#calendar",
   "etag": "\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"",
   "id": "mydomain.com_xxxxxxxxxxxxxxxxxxxxx@group.calendar.google.com",
   "summary": "test calendar title"
  }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p><strong>Update with the working code based on luc's answer below:</strong></p>
  
  <p>The resource needs to be stringified (the Google Apps Script debugger was showing that it is an object, not a string). </p>
  
  <pre><code>var calendarResource = {
         "summary": "test calendar title"
        };
  
  var fetchArgs = {};
  fetchArgs.headers = {'Authorization': 'Bearer '+ calendarOauth2Service.getAccessToken() };
  fetchArgs.method = "POST";
  fetchArgs.payload = JSON.stringify(calendarResource);
  fetchArgs.muteHttpExceptions = true;
  
  var url = 'https://www.googleapis.com/calendar/v3/calendars';
  
  var response = UrlFetchApp.fetch(url, fetchArgs);
  </code></pre>