Question

I am having issues calling Calendar:Insert via Google's API https://www.googleapis.com/calendar/v3/calendars

I don't believe there are authorization/permission issues, the access_token is acquired via a refresh_token with the following scope: "https://www.googleapis.com/auth/calendar"

When I use a valid access_token to perform GET there are no issues, but this is a POST and I consistently get this response:

 {"error": 
   { "errors": 
     [{ "domain": "global", 
       "reason": "authError", 
       "message": "Invalid Credentials",
       "locationType": "header",
       "location": "Authorization"
     }], 
     "code": 401, 
     "message": "Invalid Credentials" 
   } 
 }

Here is the Railo code I'm running, I've stripped it of all pretense and nuance:

<cfhttp url="https://www.googleapis.com/calendar/v3/calendars" charset="utf-8" method="post">
    <cfhttpparam type="header" name="Authorization" value="bearer #arguments.access_token#" />
    <cfhttpparam type="formfield" name="summary" value='hello world' />
</cfhttp>

Here is an example of a get that works just fine:

<cfhttp url="https://www.googleapis.com/calendar/v3/calendars/#arguments.calendarID#/events?access_token=#arguments.access_token#" charset="utf-8" method="get" />

So far I've attempted placing the access_token in a variety of ways. As a query parameter, as a json struct in the cfhttpparam type="body" with no luck

This stackoverflow question indicates that the Google Calendar API documentation neglects to mention a required parameter "minAccessRole". I've fiddled with that too to no avail.

Was it helpful?

Solution

Some time removed from the problem often brings clarity.

With trial and error I was able to get some error code feedback from the API. At some point revealed to me the Content-Type I was sending was "octet-stream".

I added the following line, to specify the Content-Type. I chose "application/json" since the https://developers.google.com/oauthplayground/ had that as the default content-type for the operation: calendar insert.

<cfhttpparam type="header" name="Content-Type" value="application/json" />

Then it occurred to me that I was attempting to send form fields to the API and not JSON. The final working code for callooks something like this:

<cfhttp url="https://www.googleapis.com/calendar/v3/calendars" charset="utf-8" method="post">
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="header" name="Authorization" value="bearer #arguments.access_token#" />
    <cfhttpparam type="body" value='{"summary":"newCalendar"}' />
</cfhttp>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top