Madison May
Madison May

Reputation: 2763

Python - Heroku urllib2.HTTPError

I've spent the past day or so working with a group to create a facebook app that computes a user's cluster coefficient (a measure of the interconnectedness of their friends.)
In order to accomplish this, I need to get the number of mutual friends the user with each of their friends. Unfortunately, I keep getting a urllib2.HTTPError, and I can't figure out what's going on. You can access the app (and the error message + traceback) at https://gentle-stream-3754.herokuapp.com/.

Line 168 in particular was part of the traceback. I know the code below most likely won't be helpful without context, so that's why I've included link to the app.

Could it be a problem with user permissions? Runtime?

Any help would be appreciated! Thanks in advance!

EDIT: access_token appears to be a NoneType object, but I'm not entirely sure why. Could this be causing the urllib2.HTTPError?

164 def clustering_coefficient():
165 
166     access_token = get_token()
167     g = Graph(access_token)
168     requests = [{'method': 'GET', 'relative_url': 'me/mutualfriends/%s' % x['id']} for x in g.me.friends()['data']]
169     k_v = len(requests)
170     if k_v < 2:
171         return 0.0
172 
173     n_v = 0
174     REQS_PER_POST = 50 
175     for i in range((k_v // REQS_PER_POST)+1):
176         result = g.post(batch=json.dumps(requests[i*REQS_PER_POST:(i+1)*REQS_PER_POST]))
177         for r in result:
178             body = json.loads(r['body'])
179             n_v += len(body['data'])
180 
181     return 1.0 * n_v / k_v/ (k_v-1)

Exception: HTTPError: HTTP Error 400: Bad Request

Traceback:

Traceback (most recent call last)
File "/app/lib/python2.7/site-packages/flask/app.py", line 1306, in __call__
return self.wsgi_app(environ, start_response)
File "/app/lib/python2.7/site-packages/flask/app.py", line 1294, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/app/lib/python2.7/site-packages/flask/app.py", line 1292, in wsgi_app
response = self.full_dispatch_request()
File "/app/lib/python2.7/site-packages/flask/app.py", line 1062, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/app/lib/python2.7/site-packages/flask/app.py", line 1060, in full_dispatch_request
rv = self.dispatch_request()
File "/app/lib/python2.7/site-packages/flask/app.py", line 1047, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/exampleapp.py", line 168, in clustering_coefficient
requests = [{'method': 'GET', 'relative_url': 'me/mutualfriends/%s' % x['id']} for x in g.me.friends()['data']]
File "/app/lib/python2.7/site-packages/facegraph/graph.py", line 157, in __call__
data = json.loads(self.fetch(self.url | params))
File "/app/lib/python2.7/site-packages/facegraph/graph.py", line 203, in fetch
conn = urllib2.urlopen(url, data=data)
File "/usr/local/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/local/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/usr/local/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/local/lib/python2.7/urllib2.py", line 438, in error
return self._call_chain(*args)
File "/usr/local/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/usr/local/lib/python2.7/urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)

Upvotes: 0

Views: 797

Answers (1)

girasquid
girasquid

Reputation: 15526

It looks like this is a problem with the facegraph library - it's sending a request that's receiving a 400 status code. Are you using the latest version? Is it possible something in the Facebook API changed that's causing facegraph to have problems now?

Upvotes: 1

Related Questions