使用 Mirror API 时,我从几个 API 调用(包括timeline().insert
和timeline.list
. 通过在 Python 中寻找类似问题的 SO,我怀疑这是来自服务器的某种格式错误的响应。
它似乎是随机发生的,并且可能在一段时间不使用 API 之后发生。这是一个示例堆栈跟踪:
Traceback (most recent call last):
File "service.py", line 61, in receive_message
self.process_user_chat(msg)
File "service.py", line 304, in process_user_chat
self.upsert_timeline_item(sourceItemId, body)
File "service.py", line 86, in upsert_timeline_item
new_item = self.glass_service.timeline().insert(body=body).execute()
File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 128, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/apiclient/http.py", line 676, in execute
body=self.body, headers=self.headers)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 128, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 494, in new_request
self._refresh(request_orig)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 653, in _refresh
self._do_refresh_request(http_request)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 682, in _do_refresh_request
self.token_uri, method='POST', body=body, headers=headers)
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1570, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1317, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1286, in _conn_request
response = conn.getresponse()
File "/usr/lib/python2.7/httplib.py", line 1034, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 407, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 371, in _read_status
raise BadStatusLine(line)
BadStatusLine: ''
这段代码大部分时间都有效,但每隔一段时间我就会收到这样的响应。
更新:我的代码正在创建 Google API 类并在服务期间使用它们。一旦有时间更新我的oauth2
令牌(1 小时),我会收到一次或两次此错误,然后它会再次开始工作。我能够重构我的代码,以便它在每个请求上创建 API 类,问题就消失了。这似乎是 Google API 中的一个已知错误,有关更多信息,请参阅 Anthony Tuininga 的选定答案。