简体   繁体   English

如何使用HTTParty格式化Google Calender API请求

[英]How to format a Google Calender API request using HTTParty

I am getting the following error when I make a request to the Google Calendar API. 向Google Calendar API提出请求时,出现以下错误。

{"error"=>{"errors"=>[{"domain"=>"global", "reason"=>"required", "message"=>"Missing end time."}], "code"=>400, "message"=>"Missing end time."}}

What is wrong with my formatting? 我的格式有什么问题? I've tried a ton of different layouts and can't seem to find much information about using HTTParty to make a request to a Google API. 我尝试了很多不同的布局,但似乎找不到有关使用HTTParty向Google API发出请求的更多信息。

results = HTTParty.post("https://www.googleapis.com/calendar/v3/calendars/primary/events?key=#{Rails.application.secrets.google_api_key}",
  :headers => {
    "Authorization" => "Bearer #{response["access_token"]}"
  },
  :query => {
    "end": {
      "dateTime" => "2015-05-29T09:00:00-08:00",
      "timeZone" => "America/Los_Angeles"
    },
    "start": {
      "dateTime" => "2015-05-29T09:00:00-07:00",
      "timeZone" => "America/Los_Angeles"
    },
    "summary": "TEST POST"
  }
)

Thanks in advance! 提前致谢!

Figured it out. 弄清楚了。 I needed to use a :body key and also specify JSON in the header 我需要使用:body键,并在标头中指定JSON

results = HTTParty.post("https://www.googleapis.com/calendar/v3/calendars/primary/events?key=#{Rails.application.secrets.google_api_key}",
  :headers => {
    "Authorization" => "Bearer #{response["access_token"]}",
    "Content-Type" => "application/json"
  },
  :body => {
    "end": {
      "dateTime" => "2015-05-29T09:00:00-08:00",
      "timeZone" => "America/Los_Angeles"
    },
    "start": {
      "dateTime" => "2015-05-29T09:00:00-07:00",
      "timeZone" => "America/Los_Angeles"
    },
    "summary": "TEST POST"
  }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM