我在从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":"Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request","type":"OAuthException","code":100}}
2 回答
发现redirect_uri需要一个尾部斜杠 . 以下现在有效 .
您是否可以使用现有模块(django-social-auth)进行Facebook登录?
如果没有,我将首先更改
if
循环的逻辑,因为它未能打开 . 改变它如下:在
context['code_url']
中你有redirect_uri=http://test.com
,是否应该用不同的东西替换?