如何使用python social auth在代码中通过访问令牌进行身份验证

如何使用python social auth在代码中通过访问令牌进行身份验证,python,django,facebook,python-social-auth,Python,Django,Facebook,Python Social Auth,我有一个RESTAPI,我需要通过Facebook登录API对用户进行身份验证。访问令牌应该在移动应用程序中获得(我猜),然后发送到服务器。所以我在以前的教程中找到了一些代码,但我无法让它工作。代码如下: from social.apps.django_app.utils import strategy from django.contrib.auth import login from django.views.decorators.csrf import csrf_exempt from r

我有一个RESTAPI,我需要通过Facebook登录API对用户进行身份验证。访问令牌应该在移动应用程序中获得(我猜),然后发送到服务器。所以我在以前的教程中找到了一些代码,但我无法让它工作。代码如下:

from social.apps.django_app.utils import strategy
from django.contrib.auth import login
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view, permission_classes
from rest_framework import permissions, status
from django.http import HttpResponse as Response


@strategy()
def auth_by_token(request, backend):
    user=request.user
    user = backend.do_auth(
        access_token=request.DATA.get('access_token'),
        user=user.is_authenticated() and user or None
        )
    if user and user.is_active:
        return user
    else:
        return None



@csrf_exempt
@api_view(['POST'])
@permission_classes((permissions.AllowAny,))
def social_register(request):
    auth_token = request.DATA.get('access_token', None)
    backend = request.DATA.get('backend', None)
    if auth_token and backend:
        try:
            user = auth_by_token(request, backend)
        except Exception, err:
            return Response(str(err), status=400)
        if user:
            login(request, user)
            return Response("User logged in", status=status.HTTP_200_OK)
        else:
            return Response("Bad Credentials", status=403)
    else:
        return Response("Bad request", status=400)
当我尝试发送带有参数的POST请求时,出现以下错误:

'unicode' object has no attribute 'do_auth'
我在中找到了一个例子,它使用了
@psa('social:complete')
decorator:

from django.contrib.auth import login

from social.apps.django_app.utils import psa

# Define an URL entry to point to this view, call it passing the
# access_token parameter like ?access_token=<token>. The URL entry must
# contain the backend, like this:
#
#   url(r'^register-by-token/(?P<backend>[^/]+)/$',
#       'register_by_access_token')

@psa('social:complete')
def register_by_access_token(request, backend):
    # This view expects an access_token GET parameter, if it's needed,
    # request.backend and request.strategy will be loaded with the current
    # backend and strategy.
    token = request.GET.get('access_token')
    user = backend.do_auth(request.GET.get('access_token'))
    if user:
        login(request, user)
        return 'OK'
    else:
        return 'ERROR'
从django.contrib.auth导入登录名
从social.apps.django_app.utils导入psa
#定义一个指向此视图的URL条目,称之为传递
#访问令牌参数,如?访问令牌=。URL项必须为空
#包含后端,如下所示:
#
#url(r'^register by token/(?P[^/]+)/$),
#'通过\u访问\u令牌注册\u')
@psa(“社会:完成”)
def按访问令牌注册(请求,后端):
#如果需要,此视图需要一个access_token GET参数,
#request.backend和request.strategy将与当前
#后端和策略。
token=request.GET.GET('access\u token')
user=backend.do\u auth(request.GET.GET('access\u token'))
如果用户:
登录(请求、用户)
返回“OK”
其他:
返回“错误”

但如果我需要在请求正文中传递后端名称呢?

应该是
request.backend.do\u auth(request.GET.GET('access\u token'))


我用正确的代码片段更新了文档

现在,您可以使用来自任何python social auth后端(Facebook、Google、Github等)的承载令牌/第三方访问令牌,根据django rest框架对您的用户进行身份验证,该库应为
request.backend.do_auth(request.GET.GET('access_token'))
,我已经用正确的代码片段更新了文档。你能把它作为一个答案发布吗?我需要将request=request添加到do_auth参数中——如果没有这个参数,管道social_auth.backends.pipeline.misc.save_status_to_会话将导致授权流不返回用户@如果你认为omab是有效的,请更新文档——我花了两天时间调试它!我收到此错误**403客户端错误:禁止url:access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyj1c2vyx2lkijoxocwidxnlcm5hbwuiijzdwwwwwwwwwwwwwwwwppdc50d3ilcjlehaiojljljlj1mjemj2ntc2nc2nc2nc2nc2nc2nqqqqquddyqdqdyqdyqgdtywlmnvbj9.afj9.afmbmcz00vugg246\u6xyy6x