4

我在从 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_urlhttps ://www.facebook.com/dialog/oauth?client_id=258585004264349&redirect_uri=http:
//pckandap.com&state=pk1&scope=user_likes token_urlhttps:// 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} }

4

2 回答 2

10

发现 redirect_uri 需要一个斜杠。以下现在有效。

from django.http import HttpResponse, HttpResponseRedirect
from django.template import Context, loader
import urllib, json, sys, cgi

APP_ID = 'DDDDDDDDDDDDDDD'
APP_SECRET = 'SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS'
MY_URL = 'http://pckandap.com/'

def fbauth(request):
    code = request.GET.get('code')
    if code is None:
        args = dict(client_id=APP_ID, redirect_uri=MY_URL, scope="user_likes,friends_likes")
        redirect_url = "https://graph.facebook.com/oauth/authorize?" + urllib.urlencode(args)
        return HttpResponseRedirect(redirect_url)
    else:
        args = dict(client_id=APP_ID, redirect_uri=MY_URL)
        args["client_secret"] = APP_SECRET  
        args["code"] = code
        token_url = "https://graph.facebook.com/oauth/access_token?"+urllib.urlencode(args)
        request.session['access_token'] = urllib.urlopen(token_url).read()[13:-1]
        return HttpResponseRedirect('/dash/likes')
于 2012-10-21T12:34:24.270 回答
1

您是否可以使用现有模块(django-social-auth)进行 facebook 登录?

如果没有,我将首先更改if循环的逻辑,因为它无法打开。改变它:

code = request.GET.get('code', None)

if code is not None:
    access_token = urllib.urlopen...
else:
    redirect_url = ...

在你的context['code_url']你有redirect_uri=http://test.com,应该用不同的东西代替吗?

于 2012-10-21T12:20:56.460 回答