过去一天左右,我与一个小组一起创建了一个 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)