Ruby on rails Rails通过rest客户端发布到Google日历API';错误请求';错误

Ruby on rails Rails通过rest客户端发布到Google日历API';错误请求';错误,ruby-on-rails,ruby,google-api,google-calendar-api,rest-client,Ruby On Rails,Ruby,Google Api,Google Calendar Api,Rest Client,我已经搜索了许多其他问题,我看不出我在做什么不同的事情,但当试图将新事件发布到GoogleAPI时,rest客户端将无法工作 url = "https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token=#{token}" params = {start: 'test'}.to_json response = RestClient.post url, params RestClient::BadReque

我已经搜索了许多其他问题,我看不出我在做什么不同的事情,但当试图将新事件发布到GoogleAPI时,rest客户端将无法工作

url = "https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token=#{token}"
params = {start: 'test'}.to_json
response = RestClient.post url, params

RestClient::BadRequest (400 Bad Request)

请注意,您可能对使用Google自己的Ruby客户端库而不仅仅是RestClient感兴趣:


使用RestClient解决问题

您可以从HTTP 400异常中提取错误消息

我看到的是:

auth_header 'Bearer zzzz...'
base_url = 'https://www.googleapis.com/calendar/v3/calendars/primary'
event = {'summary': 'Test RestClient event', 'start': {'dateTime': Time.now.strftime('%FT%T%z')}, 'end': {'dateTime': (Time.now + 3600).strftime('%FT%T%z')}}

begin
  RestClient.post(base_url + '/events', event.to_json, {authorization: auth_header})
rescue RestClient::BadRequest => err
  puts err.response.body
end
它将打印此HTTP 400响应:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "This API does not support parsing form-encoded input."
   }
  ],
  "code": 400,
  "message": "This API does not support parsing form-encoded input."
 }
}
这是怎么回事?RestClient假定您需要
应用程序/x-www-form-urlencoded
的请求
内容类型
,除非您另有指定

因此,如果我们添加正确的
内容类型:application/json
头,一切都会正常工作:

>> RestClient.post(base_url + '/events', event.to_json, {authorization: auth_header, content_type: :json})
=> <RestClient::Response 200 "{\n \"kind\": ...">
>RestClient.post(base_url+'/events',event.to_json,{authorization:auth_header,content_type::json})
=> 


删除了之前的答案,该答案无法解决问题

错误表示未提供所需字段或参数,提供的值无效,或提供的字段组合无效。这建议尝试切换到ssl:
PivotalTracker::Client.use_ssl=true
。这可能也有帮助。我见过其他的例子。我还尝试移动它,将其从url中取出,并将参数更改为:
params={access_-token:token,start:“test”}。更改为_-json
,但出现未经授权的错误。