Facebook OAuth 问题
我正在按照这里的说明进行操作:
http://developers.facebook.com/docs/authentication/
我想在服务器端连接到 Facebook 的图形 API。我使用的是 Django,基本上是复制这里的代码:
https://github.com/facebook/python-sdk/blob/master/examples/oauth/facebookoauth.py
这是我的代码:
def get_code(request):
c = RequestContext(request)
verification_code = request.GET.get('code',None)
args = dict(client_id=FACEBOOK_APP_ID, redirect_uri=REDIRECT)
if verification_code:
args["client_secret"] = FACEBOOK_SECRET_KEY
args["code"] = verification_code
response = cgi.parse_qs(urllib.urlopen(
"https://graph.facebook.com/oauth/access_token?" +
urllib.urlencode(args)).read())
print "------------------------"
print response
#access_token = response["access_token"][-1]
#print access_token
print "------------------------"
else:
http.HttpResponseRedirect("https://graph.facebook.com/oauth/authorize?" + urllib.urlencode(args))
总之,我成功获取了授权码,但当我尝试用它来获取访问令牌时,看到的响应是:
{
"error": {
"type": "OAuthException",
"message": "Error validating verification code."
}
}
我完全不知道发生了什么,但我在 Facebook 网站和他们提供的代码上都在按照说明操作,这在 Python 和浏览器中都不工作。有趣的是,如果我使用相同的凭证进行客户端流程,我可以通过哈希标签获取访问令牌,但这对我没有用。
另外,我是在本地测试,地址是 http://127.0.0.1:8000,并且在我的 Facebook 应用设置中正确配置了。
谢谢
更新:
我解决了,结果发现两个重定向 URL 必须完全相同。我之前使用的是:
^/facebook/auth/
和
^facebook/auth/token/
一旦我都改成:
facebook/auth/ + facebook/auth/
就成功了。