【问题标题】:python-social-auth: AttributeError at /complete/google-oauth2/: 'NoneType' object has no attribute 'provider'python-social-auth: AttributeError at /complete/google-oauth2/: 'NoneType' 对象没有属性 'provider'
【发布时间】:2016-12-17 18:13:40
【问题描述】:

我正在使用我的公司帐户(即“Google forworks”帐户)来实现 Google oauth2.0 登录到我的 django 应用程序。

“settings.py”中的管道如下所示:

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.associate_by_email',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
]

向管道添加条件后端。

if config.GCPAuthentication.AUTO_CREATE_ACCOUNTS:
    SOCIAL_AUTH_PIPELINE.extend([
    'social.pipeline.user.get_username',
    'social.pipeline.user.create_user'
])

首次尝试使用新用户登录时,出现错误: /complete/google-oauth2/ 处的 AttributeError:“NoneType”对象没有属性“provider”

有趣的是,用户正在被创建并保存在数据库中,并且在下次登录尝试时它允许我登录。

这里的抛出错误:https://github.com/omab/python-social-auth/blob/master/social/actions.py#L70

我的公司 Google 帐户可能没有任何与之关联的社交帐户(Google+ 已禁用)/相关信息。这是个问题吗?

无论如何,您能告诉我解决此问题的任何解决方法吗?

【问题讨论】:

  • get_username() 和 create_user() 返回什么?
  • 如何检查?在用户中,使用用户名作为我的电子邮件 ID 创建的新用户,这是因为我有 SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = True in settings.py
  • 我改变了函数管道的顺序并且它起作用了:我想它应该遵循这里提到的方式:github.com/omab/python-social-auth/blob/master/docs/… 谢谢@luke_aus。

标签: django python-2.7 google-oauth python-social-auth


【解决方案1】:

我遇到了来自损坏的本地数据库的相同错误。我必须清理表格:social_auth_usersocialauthdjango_session

【讨论】:

    【解决方案2】:

    作为旁注,我想补充一点,当我通过更改主键来更新我的 USER 模型时,我遇到了类似的问题。在这种情况下,仅仅为新的USER 模型运行迁移是不够的。您还必须为社交身份验证运行迁移。我是通过对整个项目的完整和强制迁移来做到的。

    【讨论】:

      【解决方案3】:

      是的,你是对的,管道中函数的顺序在这里很重要,因为一个函数的返回将输入到管道中的下一个函数。

      【讨论】:

        【解决方案4】:

        管道希望遵循需要调用函数的顺序。 对于我的解决方案,正确的顺序应该是:

        SOCIAL_AUTH_PIPELINE = [  # Note: Sequence of functions matters here.
            'social.pipeline.social_auth.social_details',  # 0
            'social.pipeline.social_auth.social_uid',  # 1
            'social.pipeline.social_auth.auth_allowed',  # 2
            'social.pipeline.social_auth.social_user',  # 3
            'social.pipeline.user.get_username',  # 4
            'social.pipeline.social_auth.associate_by_email',  # 5
            'social.pipeline.social_auth.associate_user',  # 6
            'social.pipeline.social_auth.load_extra_data',  # 7
            'social.pipeline.user.user_details',  # 8
        ]
        
        # Adding conditional functions to pipepline.
        # NOTE: Sequence of functions matters here.
        if config.GCPAuthentication.AUTO_CREATE_ACCOUNTS:
            SOCIAL_AUTH_PIPELINE.insert(6, 'social.pipeline.user.create_user')
        

        【讨论】:

        猜你喜欢
        • 2022-12-15
        • 2020-03-29
        • 2014-11-11
        • 2017-02-17
        • 2017-10-09
        • 2017-12-28
        • 2017-10-05
        • 2018-03-17
        • 2019-01-10
        相关资源
        最近更新 更多