我在从 Facebook 获取我正在尝试编写的 Django 应用程序的访问令牌时遇到问题。我的视图设置如下。
from django.http import HttpResponse, HttpResponseRedirect
from django.template import Context, loader
import urllib, json, sys
APP_ID = 'DDDDDDDDDDDDDDD'
APP_SECRET = 'SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS'
MY_URL = 'http://pckandap.com'
def fbauth(request):
template = loader.get_template('fbauth.html')
code = request.GET.get('code')
if code is None:
redirect_url = 'https://www.facebook.com/dialog/oauth?client_id='+APP_ID+'&redirect_uri='+MY_URL+'&state=pk1&scope=user_likes'
return HttpResponseRedirect(redirect_url)
else:
access_token = urllib.urlopen('https://graph.facebook.com/oauth/access_token?client_id='+APP_ID+'&redirect_uri='+MY_URL+'&client_secret='+APP_SECRET+'&code='+code)
context = Context({
'code_url' : 'https://www.facebook.com/dialog/oauth?client_id='+APP_ID+'&redirect_uri=http://test.com'+'&state=pk1&scope=user_likes',
'token_url' : 'https://graph.facebook.com/oauth/access_token?client_id='+APP_ID+'&redirect_uri='+MY_URL+'&client_secret='+APP_SECRET+'&code='+code,
'access_token' : access_token.read()
})
return HttpResponse(template.render(context))
在我转储这三个变量的模板中,我得到以下信息:
code_url:https ://www.facebook.com/dialog/oauth?client_id=258585004264349&redirect_uri=http:
//pckandap.com&state=pk1&scope=user_likes token_url:https:// graph.facebook.com/oauth/access_token?client_id=258585004264349&redirect_uri=http://pckandap.com&client_secret=0916bad6925f0df7719218bef87b9576&code=AQDiSPeG4wGLSPKLxy2P1gPWv6se46PN_-CPbUeB3ruZKmvPM7enVHc7yiiLe6goFZwG7quOokNGLY6ktOi32VX0SB5lqjbe-kvT_hxhwIYV3VkJklOpFysSWAWrCnOu5w0pYVIC5GAIpOE7QUVzq3GMf-u6W096zn_4h7X9ODjHo1qGdxUTf9KxCpDXJLzRib2YtZfpR2-RZj0tmAXoN139
access_token: {"error":{"message":"验证验证码时出错。请确保您的 redirect_uri 与您在 OAuth 对话请求中使用的相同","type":"OAuthException","code":100} }