【问题标题】:Google Calendar API V3 insert_calendar returns 503, but calendar was inserted successfullyGoogle Calendar API V3 insert_calendar 返回 503,但日历插入成功
【发布时间】:2017-08-25 22:03:06
【问题描述】:

我遇到了一个问题,我向 Google Calendar API V3 发送了一个 insert_calendar 请求,我得到了以下响应:

Sending HTTP post https://www.googleapis.com/calendar/v3/calendars?

503

#<HTTP::Message:0x000000124eb008 
  @http_header=#<HTTP::Message::Headers:0x000000124eafe0 
  @http_version="1.1", 
  @body_size=0, 
  @chunked=false,
  @request_method="POST", 
  @request_uri=#<Addressable::URI:0x9275f20 URI:https://www.googleapis.com/calendar/v3/calendars?>, 
  @request_query=nil, 
  @request_absolute_uri=nil, 
  @status_code=503, 
  @reason_phrase="Service Unavailable", 
  @body_type=nil, 
  @body_charset=nil, 
  @body_date=nil, 
  @body_encoding=#<Encoding:UTF-8>, 
  @is_request=false, 
  @header_item=[
    ["Vary", "Origin"], 
    ["Vary", "X-Origin"], 
    ["Content-Type", "application/json; charset=UTF-8"], 
    ["Content-Encoding", "gzip"], 
    ["Date", "Fri, 25 Aug 2017 20:16:34 GMT"], 
    ["Expires", "Fri, 25 Aug 2017 20:16:34 GMT"], 
    ["Cache-Control", "private, max-age=0"], 
    ["X-Content-Type-Options", "nosniff"], 
    ["X-Frame-Options", "SAMEORIGIN"], 
    ["X-XSS-Protection", "1; mode=block"], 
    ["Server", "GSE"], ["Alt-Svc", "quic=\":443\"; ma=2592000; v=\"39,38,37,35\""], 
    ["Transfer-Encoding", "chunked"]
  ], 
  @dumped=false>, 
  @peer_cert=#<OpenSSL::X509::Certificate: 
    subject=#<OpenSSL::X509::Name:0x00000012600998>, 
    issuer=#<OpenSSL::X509::Name:0x000000126009c0>, 
    serial=#<OpenSSL::BN:0x000000126009e8>, 
    not_before=2017-08-15 16:06:52 UTC, 
    not_after=2017-11-07 16:04:00 UTC
  >, 
  @http_body=#<HTTP::Message::Body:0x000000124eaf68 
    @body="{\n \"error\": {\n \"errors\": [\n{\n \"domain\": \"global\",\n \"reason\": \"backendError\",\n \"message\": \"Backend Error\"\n }\n ],\n \"code\": 503,\n \"message\": \"Backend Error\"\n }\n}\n",
    @size=0, 
    @positions=nil, 
    @chunk_size=nil
  >, 
  @previous=nil>

Caught error Server error

Error - #<Google::Apis::ServerError: Server error>

我正在使用 Google API Ruby 客户端,详细信息在这里:

google-api-client (0.13.1)
  addressable (~> 2.5, >= 2.5.1)
  googleauth (~> 0.5)
  httpclient (>= 2.8.1, < 3.0)
  mime-types (~> 3.0)
  representable (~> 3.0)
  retriable (>= 2.0, < 4.0)

我遇到的问题不是遇到错误,而是日历已成功插入。

正如您从回复中看到的那样,尽管有 503(例如 Google 日历 ID),但我没有收到任何回复告诉我它是成功的。

这对我的应用程序的影响是我不知道我是否已成功同步,事实上,通过遵循文档,我实现了指数退避,因此我继续在用户的 Google 日历上创建重复的日历.

最后,我出现了一堆孤立的日历,我必须通过字符串匹配来删除它们。

这是预期的吗?有什么办法可以缓解这种情况吗?

这种情况经常发生,并不是孤立的情况。


有问题的代码:

def handle_calendar_response(response, error)
  self.update_column('last_synced_at', Time.now.utc)
  if error.present?
    Airbrake.notify('Sync Calendar Sync Error', {
      error: error,
      message: error.message,
      calendar: self
    })

    # String match :(
    if error.message =~ /not.?found/i || error.message =~ /forbidden/i
      Airbrake.notify('Removing user deleted calendar', {
        calendar: self,
        google_calendar_id: self.google_calendar_id,
        error: error,
        message: error.message
      })
      self.publish_to_google = false
      self.google_calendar_id = nil
      self.save!
    end
  end
end

...

def insert_calendar
  @client.insert_calendar(google_calendar_object) do |response, error|
    handle_calendar_response(response, error)
    if response.present?
      self.google_calendar_id = response.id
      self.save!
    end
  end
end

这些方法来自我们数据模型中同步日历的表示形式。您可以拨打insert_calendar 插入它。我们总是对来自 Google 的响应采取相同的操作,如果我们要插入、更新或删除,我们总是调用 handle_calendar_response

【问题讨论】:

  • 你能把代码发过来吗?
  • 会的。道歉。
  • 我不认为这是预期的。您可以通过 developers.google.com/google-apps/calendar/support 向 Google 提交错误报告。看到从谷歌日历 API 返回的“后端错误”和“服务器错误”消息,看起来它已经结束了。如果响应正文中出现“后端错误”的 503 响应,也许您可​​以通过解析响应来缓解,您可以继续执行代码,就好像它成功一样。
  • 我们在哪里可以看到您的脚本?
  • 我以前也遇到过同样的问题!您可以分享您在请求中发送的正文吗?

标签: google-api calendar google-api-ruby-client google-calendar-api


【解决方案1】:

由于错误出现在 google 后端系统中,除了提交错误报告之外,您几乎无能为力。但是,您可以通过添加自己的标识符并将其放入您要创建的日历的私有 extended properties 来处理这个问题。标识符可以是日期/时间和事件主题的组合,甚至可以是随机 UIID,只要您保证它是唯一的即可。然后在收到错误时发出另一个请求之前,首先浏览用户日历并检查在同一日期是否有带有标识符的事件。这假定您具有用户日历的读取权限。希望这会有所帮助。

【讨论】:

  • 比字符串匹配更好,谢谢你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多