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

标签 python django facebook python-social-auth

我有一个 REST API,我需要通过 Facebook 登录 API 对用户进行身份验证。 Access Token 应该在移动应用程序中获取(我猜),然后发送到服务器。所以我在旧教程中找到了一些代码,但我无法让它工作。这是代码:

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'

我在 official documentation 中找到了一个例子,它使用 @psa('social:complete') 装饰器:

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'

但是如果我需要在请求正文中传递后端名称怎么办?

最佳答案

应该是request.backend.do_auth(request.GET.get('access_token')) .

我已经用正确的片段更新了文档 http://psa.matiasaguirre.net/docs/use_cases.html#signup-by-oauth-access-token https://python-social-auth.readthedocs.io/en/latest/use_cases.html#signup-by-oauth-access-token .

关于python - 如何使用 python-social-auth 在代码中通过访问 token 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26658130/

相关文章:

php - Facebook PHP API 在登录时抛出异常

java - 从 Facebook SDK 中的 strings.xml 获取值

python - pip 安装失败,出现 : OSError: [Errno 13] Permission denied on directory

python - pandas 数据框样式格式不打印指定的精度

python - 使用 Flask 0.10.1 时获取 'TypeError: ObjectId(' ') 不是 JSON 序列化'

Django 管理员 : show records for respective user only

缺少 python 配置

django - 如何解决不存在的字段的完整性错误?

html - 显示 django 模板中的图像 url

iOS Facebook SDK : show progress of received data with FBURLConnection