Search code examples
djangopython-2.7google-oauthpython-social-auth

python-social-auth: AttributeError at /complete/google-oauth2/: 'NoneType' object has no attribute 'provider'


I am using my corporate account (which is "Google for works" account) to implement Google oauth2.0 login in to my django application.

Pipeline in "settings.py" looks like:

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',
]

Adding conditional backends to pipepline.

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

While trying to login for the very first time with a new user, I am getting error: AttributeError at /complete/google-oauth2/: 'NoneType' object has no attribute 'provider'

And interestingly, user is getting created and saved in DB and on next login attempt it allows me to login.

Its throwing error here : https://github.com/omab/python-social-auth/blob/master/social/actions.py#L70

My corporate Google account might not have any social account(Google+ is disabled)/related info associated with it. Is that an issue?

In any case, can you please tell me any workaround to get rid of this issue?


Solution

  • The pipeline expects to follow the order in which functions needs to be called. To my solution right sequence should be like:

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