Search code examples
pythondjangofacebookpython-social-authdjango-socialauth

AttributeError at /account/login/oauth/complete/facebook/ 'NoneType' object has no attribute 'userprofile'


I have a problem, that instead of "User" Facebook account info is of "NoneType" type, so I can't save image as extended user model information (userprofile) after first facebook social authentication.

If any additional information is needed please give me a notice.

settings.py:

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
    'fields': 'id,name,email',
}

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.pipeline.social_auth.auth_allowed',
    'social_core.pipeline.social_auth.social_user',
    'social_core.pipeline.social_auth.associate_user',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details',
    'ridemaker.pipeline.save_profile',
)

pipeline.py:

from accounts.models import UserProfile
from social_core.backends.facebook import FacebookOAuth2, FacebookAppOAuth2
from urllib.request import urlopen
from django.template.defaultfilters import slugify
from django.core.files.base import ContentFile

def save_profile(backend, details, response, uid,\
              user, *args, **kwargs):
    if backend.__class__ == FacebookOAuth2:
        url = "http://graph.facebook.com/%s/picture?type=large" % response['id']
        avatar = urlopen(url)
        #Why is user argument "None"?
        profile = user.userprofile
        profile.image.save(slugify(user.username + " social") + '.jpg',
                        ContentFile(avatar.read()))
        profile.save()

Solution

  • You are overriding the SOCIAL_AUTH_PIPELINE in settings and have missed the pipeline social_core.pipeline.user.create_user which creates the user. Put below two pipelines after social_core.pipeline.social_auth.social_user in SOCIAL_AUTH_PIPELINE:

    'social_core.pipeline.user.get_username',
    'social_core.pipeline.user.create_user',