python-social-auth + mobile app

后端 未结 1 948
醉话见心
醉话见心 2021-01-15 10:15

I have a django app and I create API for mobile app. When it comes to user authentication I simple gets login + pass and do standard django login stuff. When user is logged

相关标签:
1条回答
  • 2021-01-15 10:53

    The documentation describes how you can simply provide the access_token to authenticate/create a user.

    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 = request.backend.do_auth(request.GET.get('access_token'))
        if user:
            login(request, user)
            return 'OK'
        else:
            return 'ERROR'
    
    0 讨论(0)
提交回复
热议问题