HTTP 错误 403 Forbidden with urllib2 but not with urllib with facebook graph api
我有以下带有 urllib2 的代码,它会打印 HTTP Error 403: Forbidden 但如果我使用 urllib 来获取 url,我不会看到任何错误,而且会得到我的朋友列表。在这两种情况下使用的访问令牌是相同的。
url = 'https://graph.facebook.com/me/friends/'
params = {'access_token': 'a valid access-token...', 'fields': 'id,name,birthday'}
req = urllib2.Request(url, data=urllib.urlencode(params))
try:
con = urllib2.urlopen( req )
print con.read()
except Exception as excp:
print excp.read()
请提出可能有什么问题。
I have the following code with urllib2 which prints HTTP Error 403: Forbidden but if I use urllib instead to fetch url, I don't see any error and I do get a list of my friends. The access token used is same in both the cases.
url = 'https://graph.facebook.com/me/friends/'
params = {'access_token': 'a valid access-token...', 'fields': 'id,name,birthday'}
req = urllib2.Request(url, data=urllib.urlencode(params))
try:
con = urllib2.urlopen( req )
print con.read()
except Exception as excp:
print excp.read()
Please suggest what might be wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题现在已经解决了。问题在于请求应该是 GET 而不是 POST,因此所有查询参数都应该通过 url 传递,而不是作为 POST 数据传递。因此,就我而言,要交朋友,代码将如下所示:
希望它可以帮助某人。
This one is solved now. The trouble was that the request should be GET not POST and thus all the query parameters should be passed with url instead of being passed as POST data. So in my case the to get friends the code would look something like this:
Hope it helps someone.