alvarodlg / google-api-python-client

Automatically exported from code.google.com/p/google-api-python-client
Other
0 stars 0 forks source link

BadStatusLine raised on a request (+headers) that works elsewhere. #304

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Calling the Google Analytics Data API, our code makes three main requests for 
three different pagePaths.  The first and third calls complete successfully but 
the second fails with a BadStatusLine on every run.

I've turned on logging and it tells me:

send: 'GET 
/analytics/v3/data/ga?sort=-ga%3Apageviews&max-results=100000&dimensions=ga%3Apa
gePath&start-date=2013-10-01&ids=ga%3A21906051&metrics=ga%3Apageviews%2Cga%3Avis
its&filters=ga%3ApagePath%3D%7E%2Fdata.gov.uk%2Fpublisher%2F%5Ba-z0-9%5C-_%5D%2B
%24&alt=json&end-date=2013-10-03 HTTP/1.1\r\nHost: 
www.googleapis.com\r\ncontent-length: 0\r\nauthorization: Bearer 
ya29.REST_OF_TOKEN\r\naccept-encoding: gzip, deflate\r\naccept: 
application/json\r\nuser-agent: google-api-python-client/1.2 (gzip)\r\n\r\n'
reply: ''

So understandable that it got a BadStatusLine with an empty response.  However 
I translated that call into the following curl request which completes 
successfully and shows the data I would expect.

curl -H "content-length: 0" -H "accept: application/json" -H "accept-encoding: 
gzip, deflate" -H "user-agent: google-api-python-client/1.2 (gzip)" -H 
"Authorization: Bearer ya29.REST_OF_TOKEN" 
"https://www.googleapis.com/analytics/v3/data/ga?sort=-ga%3Apageviews&max-result
s=100000&dimensions=ga%3ApagePath&start-date=2013-10-01&ids=ga%3A21906051&metric
s=ga%3Apageviews%2Cga%3Avisits&filters=ga%3ApagePath%3D%7E%2Fdata.gov.uk%2Fpubli
sher%2F%5Ba-z0-9-_%5D%2B%24&alt=json&end-date=2013-10-03"

I also get a valid response when I do the following in the repl:

>>> import requests
>>> headers['authorization'] = "Bearer ya29.REST_OF_TOKEN"
>>> r = 
requests.get("https://www.googleapis.com/analytics/v3/data/ga?sort=-ga%3Apagevie
ws&max-results=100000&dimensions=ga%3ApagePath&start-date=2013-10-01&ids=ga%3A21
906051&metrics=ga%3Apageviews%2Cga%3Avisits&filters=ga%3ApagePath%3D%7E%2Fdata.g
ov.uk%2Fpublisher%2F%5Ba-z0-9-_%5D%2B%24&alt=json&end-date=2013-10-03", 
headers=headers)
>>> r
<Response [200]>

Original issue reported on code.google.com by rossdjo...@gmail.com on 3 Oct 2013 at 2:40

GoogleCodeExporter commented 8 years ago
I will also get this error periodically with the Drive API. It will 
sporadically happen, but otherwise not happen most of the time. It's as if 
there's a malformed response. I'll either do the same operation again, or 
remount, and it'll be fine.

ERROR Could not send delete for entry with ID 
[0B2iR2qHm9cDTdWstXzczV2NNdGs].#012
Traceback (most recent call last):
 File "/usr/local/lib/python2.7/dist-packages/gdrivefs-0.13.4-py2.7.egg/gdrivefs/gdtool/drive.py", line 685, in remove_entry
  result = client.files().delete(args).execute()
 File "/usr/local/lib/python2.7/dist-packages/google_api_python_client-1.2-py2.7.egg/oauth2client/util.py", line 132, in positional_wrapper
  return wrapped(args, *kwargs)
 File "/usr/l ocal/lib/python2.7/dist-packages/google_api_python_client-1.2-py2.7.egg/apiclient/http.py", line 716, in execute
  body=self.body, headers=self.headers)
 File "/usr/local/lib/python2.7/dist-packages/google_api_python_client-1.2-py2.7.egg/oauth2client/util.py", line 132, in positional_wrapper
  return wrapped(args, *kwargs)
 File "/usr/local/lib/python2.7/dist-packages/google_api_python_client-1.2-py2.7.egg/oauth2client/client.py", line 490, in new_request
  redirections, connection_type)
 File "/usr/local/lib/pytho n2.7/dist-packages/httplib2-0.8-py2.7.egg/httplib2/init.py", line 1570, in request
  (response, content) = self.request(conn, authority, uri, requesturi, method, body, headers, redirections, cachekey)
 File "/usr/local/lib/python2.7/dist-packages/httplib2-0.8-py2.7.egg/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-0.8-py2.7.egg/httplib2/init.py", line 1286, in connrequest
  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.readstatus()
 File "/usr/lib/python2.7/httplib.py", line 371, in readstatus
  raise BadStatusLine(line)

 BadStatusLine: ''

Very annoying.

Original comment by myselfasunder@gmail.com on 14 Nov 2013 at 2:09

GoogleCodeExporter commented 8 years ago
Also, after reading this SO post, there's a chance that the token expires 
mid-operation: 
http://stackoverflow.com/questions/17180433/google-mirror-api-throwing-badstatus
line-exception-python . In their case, it's a very-long upload. Theoretically, 
though, it could occur during a short operation.

Original comment by myselfasunder@gmail.com on 14 Nov 2013 at 2:12

GoogleCodeExporter commented 8 years ago
Eventually I switched to only using the google-api lib to process the refresh 
token (before each request, I only make 3 a day), and make the http requests 
myself with python-request 
(https://github.com/datagovuk/ckanext-ga-report/blob/master/ckanext/ga_report/do
wnload_analytics.py#L251-L280) , and I've not had a problem since. No idea if 
it is related or not.

BadStatusLine gets thrown when the server returns nothing, not even an error 
response code, and so I imagine that somewhere on the analytics back-end it is 
failing to provide any response at all. Whatever the reason, I'm not sure this 
is a 'working as intended' ;) A 502, or 504 would be nicer.

Original comment by rossdjo...@gmail.com on 14 Nov 2013 at 2:26

GoogleCodeExporter commented 8 years ago
Yeah I have the same problem.  I have had some success by just trying twice, 
but even them it doesn't really work:

 try:
            return request.execute()
        except httplib.BadStatusLine as e:
            print e.line, e.message
            print '[GOOGLE_CALENDAR]: Got BadStatusLine.  Retrying...'
            try:
                return request.execute()
            except httplib.BadStatusLine as e:
                print '[GOOGLE_CALENDAR]: Got BadStatusLine again! Raising.'
                raise

Original comment by Dan.r.sc...@gmail.com on 13 Oct 2014 at 12:37