Python - Heroku urllib2.HTTPError

时间:2012-07-12 16:21:08

标签: python heroku urllib2 http-error

我花了一天左右的时间与一个小组合作创建一个Facebook应用程序来计算用户的群集系数(衡量他们朋友的相互关联程度。)
为了实现这一点,我需要与他们的每个朋友一起获得用户的共同朋友的数量。不幸的是,我一直得到一个urllib2.HTTPError,我无法弄清楚发生了什么。您可以在https://gentle-stream-3754.herokuapp.com/访问该应用(以及错误消息+追溯)。

第168行特别是追溯的一部分。我知道如果没有上下文,下面的代码很可能没有用,所以这就是为什么我已经包含了应用程序的链接。

用户权限可能有问题吗?运行?

任何帮助将不胜感激!提前谢谢!

编辑:access_token似乎是一个NoneType对象,但我不完全确定原因。这会导致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)

异常:HTTPError:HTTP错误400:错误请求

回溯:

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)

1 个答案:

答案 0 :(得分:1)

看起来这是facegraph库的一个问题 - 它正在发送一个接收400状态代码的请求。你使用的是最新版本吗?是否有可能Facebook API中的某些内容发生变化,导致facegraph现在出现问题?