Facebook发布HTTP错误400:错误的请求

时间:2012-06-28 08:37:41

标签: python facebook-graph-api urllib2 facebook-opengraph

嘿我试图通过python的urllib2库向Facebook发布一个分数。

import urllib2,urllib

url = "https://graph.facebook.com/USER_ID/scores"
data = {}
data['score']=SCORE
data['access_token']='APP_ACCESS_TOKEN'
data_encode = urllib.urlencode(data)
request = urllib2.Request(url, data_encode)
response = urllib2.urlopen(request)
responseAsString = response.read()

我收到此错误:

   response = urllib2.urlopen(request)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 124, in urlopen
    return _opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 389, in open
    response = meth(req, response)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 502, in http_response
    'http', request, response, code, msg, hdrs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 427, in error
    return self._call_chain(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 361, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 510, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request

不确定这是否与Facebook的Open Graph或不正确的urllib2 API使用有关。

2 个答案:

答案 0 :(得分:0)

您可能必须提供用户:代理作为某些浏览器。我记得在某些网站上运行抓取工具时遇到类似的错误,因为它检测到没有浏览器正在调用它。

答案 1 :(得分:0)

我运行了你的代码并得到了同样的错误(正文中没有更多的错误,会在评论中发布但我还不能猜)所以我用谷歌搜索“发布facebook得分。”

我相信您需要首先授予您的应用程序发布分数的权限,除非您已经这样做了。请参阅http://developers.facebook.com/blog/post/539/