title: Rails 4: Creating Google Calendar event
body: <p>I'm trying to insert Google Calendar events as illustrated <a href="https://developers.google.com/google-apps/calendar/v3/reference/events/insert" rel="nofollow">here</a>. If I send the request with values hardcoded like the example it works fine:</p>
  
  <pre><code>event = {'summary' =&gt; 'Summary',
           'location' =&gt; 'Location',
           'start' =&gt; { 'dateTime' =&gt; '2011-06-03T10:00:00.000-07:00' },
           'end' =&gt; { 'dateTime' =&gt; '2011-06-03T10:25:00.000-07:00' }}
  </code></pre>
  
  <p>But it fails silently if I try to send variables:</p>
  
  <pre><code>event = {'summary' =&gt; "#{params[:summary]}",
           'location' =&gt; "#{params[:location]}",
           'start' =&gt; { 'dateTime' =&gt; "#{params[:start_time]}" },
           'end' =&gt; { 'dateTime' =&gt; "#{params[:start_time]}" }}
  </code></pre>
  
  <p>It also fails if I format this way:</p>
  
  <pre><code>event = "{
            \"summary\": \"#{params[:summary]}\",
            \"location\": \"#{params[:location]}\",
            \"start\": { \"dateTime\": \"#{params[:start_time]}\" },
            \"end\": { \"dateTime\": \"#{params[:end_time]}\" }
          }"
  </code></pre>
  
  <p>I can get it to successfully post with the <code>start_time</code> and <code>end_time</code> formatted like this: <code>Date.today.strftime("%Y-%m-%dT%l:%M:%S.000-07:00")</code>, but that's less-than-ideal because I believe the <code>start_time</code> and <code>end_time</code> params are already <code>Time</code> objects.</p>
  
  <p>The form is passing the parameters because I get this in the Rails console (<code>authenticity_token</code> omitted for brevity):</p>
  
  <pre><code>Started POST "/events" for 127.0.0.1 at 2015-04-15 08:53:36 -0600
  Processing by EventsController#create as HTML
    Parameters: {"utf8"=&gt;"✓",
                 "authenticity_token"=&gt;"...",
                 "event"=&gt;{"summary"=&gt;"Summary",
                           "start_time(1i)"=&gt;"2015",
                           "start_time(2i)"=&gt;"4",
                           "start_time(3i)"=&gt;"15",
                           "start_time(4i)"=&gt;"14",
                           "start_time(5i)"=&gt;"54",
                           "end_time(1i)"=&gt;"2015",
                           "end_time(2i)"=&gt;"4",
                           "end_time(3i)"=&gt;"15",
                           "end_time(4i)"=&gt;"14",
                           "end_time(5i)"=&gt;"55",
                           "location"=&gt;"Location"},
                 "commit"=&gt;"Create Event"}
  </code></pre>
  
  <p>Here is the relevant code:</p>
  
  <pre><code>client = Google::APIClient.new({:application_name =&gt; "PlannerApp",
                                  :application_version =&gt; "1.0"})
  client.authorization.access_token = t.fresh_token
  service = client.discovered_api('calendar', 'v3')
  client.execute(:api_method =&gt; service.events.insert,
                 :parameters =&gt; {'calendarId' =&gt; 'primary'},
                 :body =&gt; JSON.dump(event),
                 :headers =&gt; {'Content-Type' =&gt; 'application/json'})
  </code></pre>
  
  <p>And here is what I get from Google (<code>Authorization</code> omitted for brevity):</p>
  
  <pre><code>Google::APIClient::Request Sending API request get https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest {
    "User-Agent"=&gt;"PlannerApp/1.0 google-api-ruby-client/0.8.5 Mac OS X/10.10.2\n (gzip)",
    "Accept-Encoding"=&gt;"gzip",
    "Content-Type"=&gt;""}
  Google::APIClient::Request Result: 200 {
    "expires"=&gt;"Wed, 15 Apr 2015 14:58:37 GMT",
    "date"=&gt;"Wed, 15 Apr 2015 14:53:37 GMT",
    "etag"=&gt;"\"ye6orv2F-1npMW3u9suM3a7C5Bo/snCOExYvhrzEZTGzdubZtafuHVU\"",
    "vary"=&gt;"Origin, X-Origin",
    "content-type"=&gt;"application/json; charset=UTF-8",
    "x-content-type-options"=&gt;"nosniff",
    "x-frame-options"=&gt;"SAMEORIGIN",
    "x-xss-protection"=&gt;"1; mode=block",
    "content-length"=&gt;"93847",
    "server"=&gt;"GSE",
    "cache-control"=&gt;"public, max-age=300, must-revalidate, no-transform",
    "age"=&gt;"0",
    "alternate-protocol"=&gt;"443:quic,p=0.5",
    "connection"=&gt;"close"}
  Google::APIClient::Request Sending API request post https://www.googleapis.com/calendar/v3/calendars/primary/events {
    "User-Agent"=&gt;"PlannerApp/1.0 google-api-ruby-client/0.8.5 Mac OS X/10.10.2\n (gzip)",
    "Content-Type"=&gt;"application/json",
    "Accept-Encoding"=&gt;"gzip",
    "Authorization"=&gt;"Bearer ...",
    "Cache-Control"=&gt;"no-store"}
  </code></pre>
  
  <p>How should I be formatting these <code>event</code> hash values to get a successful <code>POST</code>?</p>
  
  <p><strong>SOLUTION:</strong></p>
  
  <pre><code>event = {'summary' =&gt; "#{params[:event][:summary]}",
           'location' =&gt; "#{params[:event][:location]}",
           'start' =&gt; { 'dateTime' =&gt; Time.new(params['event']['start_time(1i)'],
                                               params['event']['start_time(2i)'],
                                               params['event']['start_time(3i)'],
                                               params['event']['start_time(4i)'],
                                               params['event']['start_time(5i)'])
                                          .to_datetime.rfc3339,
                        'timeZone' =&gt; "America/Denver" },
           'end' =&gt; { 'dateTime' =&gt; Time.new(params['event']['end_time(1i)'],
                                             params['event']['end_time(2i)'],
                                             params['event']['end_time(3i)'],
                                             params['event']['end_time(4i)'],
                                             params['event']['end_time(5i)'])
                                        .to_datetime.rfc3339,
                      'timeZone' =&gt; "America/Denver" }}
  </code></pre>