【问题标题】:Using Python-social-auth the social-user is not being set in the pipeline使用 Python-social-auth 未在管道中设置社交用户
【发布时间】:2015-07-23 15:17:11
【问题描述】:
AttributeError at /complete/google-oauth2/

'NoneType' object has no attribute 'provider'

这发生在https://github.com/omab/python-social-auth/blob/master/social/actions.py#L69这一行的新用户或已经注册的用户身上,这是一个新问题。它在我使用自定义管道时出现,但它以前可以工作,现在恢复到最基本的 python-social-auth 设置不起作用。

即使使用重新加载的数据库和刷新的同步数据库。 (这是 Django 1.4.20,我们正在升级的旧项目)。

问题很清楚,无论在默认管道中的什么地方,要在 user 上设置的 social-user 都不起作用,但我对 python-social-auth 代码的理解不够好,我在这里绕圈子.任何指点或帮助将不胜感激。

出现在我的堆栈跟踪中的 500 以防万一。

[23/Jul/2015 12:00:32] "GET /complete/google-oauth2/?state=MRFnOQG6Cv5Cmb1xqdcp53C33dDrFf7C&code=4/2bgjgYgxFtqB2c10BInigkigGfy4ZNOSYBqyHBUdLGo&authuser=0&prompt=consent&session_state=4c6c24bdd3100a506c4744be8ab9b793ed6399d5..a5f9 HTTP/1.1" 500 148571

这是代码阻塞的数据,这显然是因为管道的某些先前部分没有将“social_user”放入用户中。但我不知道它会在哪里失败,是否有更熟悉的人对默认管道可能失败的任何建议?

partial None
is_authenticated    False
args    ()
is_new  False
redirect_value  ''
user    <User: 4o5bb5o5>
social_user None
kwargs  {}
login   <function _do_login at 0xaef3de9c>
backend <social.backends.google.GoogleOAuth2 object at 0x9e68fac>
data    <QueryDict: {u'state': [u'dwMOHkBxsVgvj4mwL54LXqihzJVYMWTV'], u'code': [u'4/fjOVlVyAK5mS_mcv7uHVWQsFycpjlbB_YOy4F7omIIY'], u'prompt': [u'none'], u'session_state': [u'ab65081ee8e4a3720d5963deced4cabc7b259a85..8f10'], u'authuser': [u'0']}>
redirect_name   
'next'

当我导航到主页时,我已使用 gmail 帐户正常登录。 (尽管某些在 create_user 之后发生的 post_save 函数不会触发,但我认为这是因为它在管道中间崩溃了)。然后,当我注销并重新登录时,它再次在同一页面上崩溃。

【问题讨论】:

    标签: python django python-social-auth


    【解决方案1】:

    可能为时已晚,但这会对其他人有所帮助。 我在我的自定义应用程序中使用了它。

    如图here, ID_KEY 设置为 Nonehere 我用pdb 看到ID_KEY 设置为id。自然,这个框架期望 id 作为在 user_data 期间服务器响应的关键之一。

    这是我的自定义后端的样子,

    class CustomOAuth2(BaseOAuth2):
        """Custom OAuth authentication backend"""
        name = 'Custom'
        ID_KEY = 'email'  #<===== SET THIS TO UNIQUE ATTRIBUTE IF ID IS NOT RETURNED
        AUTHORIZATION_URL = 'https://auth.Custom.org/oauth2/authorize'
        ACCESS_TOKEN_URL = 'https://auth.Custom.org/oauth2/token'
        ACCESS_TOKEN_METHOD = 'POST'
        REVOKE_TOKEN_URL = '= https://auth.Custom.org/oauth2/revoke'
        REVOKE_TOKEN_METHOD = 'POST'
        USER_DATA_URL = "https://api.Custom.org/user/"
        SCOPE_SEPARATOR = ','
    
        def get_user_details(self, response):
            """Return user details from Custom account"""
            return {'username': response.get('login'),
                    'email': response.get('email') or '',
                    'first_name': response.get('name')}
    
        def user_data(self, access_token, *args, **kwargs):
            """Loads user data from service"""
            try:
                headers = {'Authorization': 'Bearer %s' %access_token}
                headers['Accept'] = 'application/json'
                response = requests.get(self.USER_DATA_URL, headers=headers)
                d = json.loads(response.content)
                return d 
            except ValueError:
                return None
    

    【讨论】:

      【解决方案2】:

      我通过注释导致问题的 actions.py 中的第 68 和 69 行使其正常工作。我知道这不是一个解决方案,但如果你需要让它工作,这可能是某种解决方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-24
        • 1970-01-01
        • 1970-01-01
        • 2019-10-19
        相关资源
        最近更新 更多