python-social-auth: AttributeError at /complete/go

2019-08-01 19:02发布

问题:

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?

回答1:

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


回答2:

Yes you are right, Sequence of the function in the pipeline matters here as return of one function will be input to the next function in pipeline.



回答3:

As a sidenote I want to add that I faced a similar issue when I updated my USER model by changing it's primary key. In such scenarios, simply running migrations for the new USER model will not suffice. You will have to run migrations for the social auth as well. I did it by complete & forced migration of the entire project.