coldfusion – Google日历OAuth access_token不适用于POST功能

我在调用日历时遇到问题:通过Google的API插入

https://www.googleapis.com/calendar/v3/calendars

我不相信存在授权/权限问题,access_token是通过具有以下范围的refresh_token获取的:“https://www.googleapis.com/auth/calendar

当我使用有效的access_token执行GET时没有问题,但这是一个POST,我一直得到这个响应:

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

这是我正在运行的Railo代码,我已经删除了所有伪装和细微差别:

<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>

这是一个可以正常运行的get的示例:

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

到目前为止,我已尝试以各种方式放置access_token.作为查询参数,作为cfhttpparam中的json结构,类型=“body”,没有运气

This stackoverflow question表示Google Calendar API文档忽略了提及必需参数“minAccessRole”.我也摆弄了这个也无济于事.

最佳答案 从问题中删除一段时间通常会带来清晰度.

通过反复试验,我能够从API获得一些错误代码反馈.在某些时候,我发现我发送的内容类型是“八位字节流”.

我添加了以下行,以指定Content-Type.我选择“application / json”,因为https://developers.google.com/oauthplayground/将其作为操作的默认内容类型:日历插入.

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

然后我发现我试图将表单字段发送到API而不是JSON.
callooks的最终工作代码如下:

<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>
点赞