【问题标题】:python-social-auth: request does not include session and meta fieldspython-social-auth:请求不包括会话和元字段
【发布时间】:2016-08-21 12:32:17
【问题描述】:
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 1.9,但此代码因 'GET' 不存在而失败,我像这样重写此函数:

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)

因此,我有重定向循环,并且服务器返回带有太多重定向的 http 错误。

毕竟我尝试编写自己的后端:

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 

这是视图:

@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)

结果:“QueryDict”对象没有“会话”属性

UserLogin 我已在 setting.py 中添加到管道的末尾

现在我有了这个:

@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)

但是现在:'QueryDict' 对象没有属性 'META' 几天我无法设置这个s..也许我做错了什么?

解决方案:我们必须使用 strategy.request 来访问 django 请求

【问题讨论】:

  • 无法理解:traceback 中的请求信息包含我需要的所有信息,包括 META 和 sessionid。所以它的管道没有为我返回任何信息?
  • 我遇到了类似的问题:stackoverflow.com/questions/38973264/…。请求中不存在会话和用户。你知道为什么吗?
  • 也许我们可以用信号来解决这个问题,比如 'auth:complete',但是我在 python-social-auth 中看不到任何信号。现在我正在寻找 psa 和部分装饰器
  • github.com/omab/python-social-auth/issues/978 我们必须使用 strategy.request 来访问 django 请求。现在尝试完成它
  • @МаксимСтукало 这个策略对象在哪里?

标签: python django session metadata python-social-auth


【解决方案1】:

如果其他人遇到这个问题,请在 github 上查看此问题:Request object not passed in pipeline。使用strategy.request 使请求对象可用。 strategy 将自动传递到您的管道。

像这样使用它:

def my_pipeline(strategy, *args, **kwargs):
    request = strategy.request
    # something...

【讨论】:

    猜你喜欢
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 2016-10-03
    • 2015-07-31
    相关资源
    最近更新 更多