Google Mirror API throwing BadStatusLine exception (Python)

自作多情 提交于 2019-12-06 06:33:40

问题


When using the Mirror API, I randomly get a "BadStatusLine" exception from several API calls, including timeline().insert and timeline.list. From trolling around SO for simliar problems in python, I suspect this is some kind of malformed response from the server.

It seems to happen randomly and possibly after a period of not using the API. Here's a sample stack trace:

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

This code works most of the time, but every once in a while I get a response like this.


Update: My code was creating the Google API classes and using them for the duration of the service. Once it got time to renew my oauth2 token (1 hour) I'd get this error once or twice then it would start working again. I was able to refactor my code so it creates the API classes on each request and the problem has gone away. This appears to be a known bug in Google's API, see the selected answer from Anthony Tuininga for more information.


回答1:


This problem appears to be a known issue. I have experienced it myself consistently. If a single file takes more than 1 hour to upload, then this error occurs. If a different file is uploaded the API refreshes the token properly. See this bug for more information. It is apparently not fixed yet and there is no ETA on when it will be fixed. :-(

https://code.google.com/p/google-api-python-client/issues/detail?id=231




回答2:


The status line is the first line returned by an HTTP response. It contains the status code like 200, 404, 500, etc. If the httplib can't read the status line, it can't read anything about it.

The problem is often caused by a bad HTTP request or a server problem. Try making the request from your browser to see what it shows you. Alternatively, you can use curl or wget to make the request.

Your stack trace shows oauth2client so you must be attempting the OAuth 2 protocol. Maybe the previous request caused the error, and this one simply met a closed door at the server. Logging all requests and responses may help you see what caused the failure.



来源:https://stackoverflow.com/questions/17180433/google-mirror-api-throwing-badstatusline-exception-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!