0
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'

This example from different resources. I use django 1.9, but this code fails with 'GET' doesn't exists and i rewrite this function like this:

def UserLogin(request, backend, details, response, *args, **kwargs):
        # 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 = response['access_token']
    user = backend.do_auth(token)
    arg = {}
    arg['user'] = user

    if user:
        login(request, user)
    return HttpResponseRedirect('/', arg)

As a result i have loop of redirect and server return http error with too many redirects.

After all i try write my own backend:

class AuthBackend(object):
    def authenticate(self, access_token, id, add_info, **kwargs):
        print 'authenticate backend'
        user = None
        print 'access_token='+access_token
        try:
            prof = qProfile.objects.get(facebook_uid=id)
            user = User.objects.get(pk=prof.muser_id)
            print "i've found this user and return pk"
        except Exception as Error:
            try:
                print 'kwargs:'+str(kwargs)
                print "can't get user and have this error:"+str(Error)
                user = User.objects.create(email = add_info['email'])
                user.first_name = add_info['first_name']
                user.last_name  = add_info['last_name']
                user.username   = add_info['username']

                user.save()

                new_profile = qProfile.objects.create(facebook_uid=id, muser_id=user.pk)
                new_profile.token = access_token

                new_profile.save()

            except Exception as Error2:
                print 'new profile error:'+str(Error2)

        finally:
            return user
            print 'user:'+str(user)

    def get_user(self, user_id):
        try:
            prof = qProfile.objects.get(facebook_uid=id)
            user = User.objects.get(pk=prof.muser_id)
            return user
        except User.DoesNotExist:
            return None 

This is view:

@csrf_exempt
def UserLogin(request, backend, details, response, *args, **kwargs):
    arg = {}
    arg['user'] = authenticate(access_token = response['access_token'], add_info = details, id = response['id'])
    print "user:='"+str(arg['user'])+"'"
    request['user'] = arg['user']
    try:
        login(request, arg['user'])
    except Exception as Error:
        print 'auth failed, error:'+ str(Error)
    finally:
        return HttpResponseRedirect('/', arg)

Resault: 'QueryDict' object has no attribute 'session'

UserLogin i've added to the end of the pipeline in setting.py

Now i have this:

@csrf_exempt
def UserLogin(request, backend, details, response, *args, **kwargs):
    arg = {}
    arg['user'] = authenticate(access_token = response['access_token'], add_info = details, id = response['id'])
    request.session = SessionStore()
    request.session.time = response['access_token']
    request.session.save()
    raise sdfds
    print "user:='"+str(arg['user'])+"'"
    request['user'] = arg['user']
    try:
        login(request, arg['user'])
    except Exception as Error:
        print 'auth failed, error:'+ str(Error)
    finally:
        return HttpResponseRedirect('/', arg)

But now: 'QueryDict' object has no attribute 'META' Few days i cann't set up this s.. Maybe i do something wrong?

Solution: we must use strategy.request to access django request

  • Cann't understand: request information in traceback consist all information that i need including META and sessionid. So it's pipeline doesn't return for me any information? – Максим Стукало Aug 21 '16 at 13:33
  • I am getting similar problem: http://stackoverflow.com/questions/38973264/on-upgrading-python-social-auth-from-0-1-17-to-0-2-4-session-attribute-is-not-p . Both session and user are not present in request. Do you have any idea why? – Sid Aug 22 '16 at 06:27
  • Maybe we can solve this problem with signals, like 'auth:complete', but i don't see any signals in python-social-auth. Now i'm looking for psa and partial decorators – Максим Стукало Aug 22 '16 at 08:35
  • 1
    https://github.com/omab/python-social-auth/issues/978 We must use strategy.request to access django request. Now wiil try finish it – Максим Стукало Aug 22 '16 at 08:52
  • @МаксимСтукало where is this strategy object? – Akash Tomar Jun 10 '17 at 13:11

1 Answers1

3

For someone else stumbling upon this question, see this issue on github: Request object not passed in pipeline. The request object is made available using strategy.request. The strategy will be automatically passed to your pipeline.

Use it like this:

def my_pipeline(strategy, *args, **kwargs):
    request = strategy.request
    # something...
xyres
  • 20,487
  • 3
  • 56
  • 85