【问题标题】:On upgrading python-social-auth from 0.1.17 to 0.2.4, session attribute is not provided in request, to custom pipeline method?在将 python-social-auth 从 0.1.17 升级到 0.2.4 时,请求中未提供会话属性,以自定义管道方法?
【发布时间】:2016-12-22 17:34:14
【问题描述】:

我的社交验证管道如下:

SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'social.pipeline.social_auth.associate_by_email',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
'useraccounts.models.create_user_profile'
)

当 'useraccounts.models.create_user_profile' 执行以下操作时:

request = kwargs.get('request')
do_something(session_key=request.session.session_key)

以下错误来了:

'QueryDict' object has no attribute 'session'

'Request' 字典具有以下键的元素:state、code、prompt、session_state、authuser

我使用的是 Django 版本 1.5.5。问题来了,因为我已将 python-social-auth 从 v0.1.17 升级到 v0.2.4

【问题讨论】:

  • request = kwargs.get('request') 不是QuerySet,它是QueryDict。你可以这样使用:request['session']['session_key'].
  • 请求没有任何名为会话的密钥,因此更改访问方法没有任何好处。虽然我尝试并得到错误: MultiValueDictKeyError: "Key 'session' not found in
  • 打印请求结果。
  • 请求:
  • 我注释掉了上面访问 request.session 的行,后来代码中断说 request.user 不存在。请求和用户都是由中间件设置的,但我再次检查并在设置中提到了它们。这可能有助于隔离问题

标签: django python-social-auth


【解决方案1】:

https://github.com/omab/python-social-auth/issues/978

尝试使用

strategy.request

访问 django 请求。

【讨论】:

  • 这看起来很有趣,应该在文档中提到。另外,我的代码能够在升级之前从 kwargs 请求中获取“会话”和“用户”,那么为什么在升级之后会发生这种情况,知道吗?
  • 抱歉,无法回答你的问题,这是我第一次使用这个模块 :) 我的身份验证工作正常,你呢?
  • 我实施了这个建议,现在一切正常。非常感谢!
【解决方案2】:

这可能是因为 user_details 函数没有将任何内容传递给管道中的下一个函数。根据您的要求,您可能需要更改管道中函数的顺序:

检查这个: https://github.com/omab/python-social-auth/blob/master/social/pipeline/user.py#L75

【讨论】:

    猜你喜欢
    • 2014-10-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 2014-01-28
    • 1970-01-01
    相关资源
    最近更新 更多