AttributeError: 'DjangoStrategy' object has no attribute 'backend' python social auth

帅比萌擦擦* 提交于 2019-12-05 01:39:29

问题


This worked last week. Maybe I did something wrong and messed it up somewhere else, or maybe it is a bug, or maybe it is just an update and I missed it while reading the docs.

I have a pipeline that gets the user's avatar and saves the URL:

def get_avatar(strategy, details, response, user, *args, **kwargs):
    url = None
    if strategy.backend.name == 'facebook':
        url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
    elif strategy.backend.name == "twitter":
        if response['profile_image_url'] != '':
            url = response['profile_image_url']
    elif strategy.backend.name == "google-oauth2":
        if response['image'].get('url') is not None:
            url = response['image'].get('url')

It used to work, now, it gives me the error:

 'DjangoStrategy' object has no attribute 'backend'

Please help, some beta users are already using my website and for the moment, they don't have a profile image.


回答1:


other solution :

def get_profile_picture(backend, user, response, details, *args, **kwargs):
    url = None
    profile = UserProfile.objects.get_or_create(user = user)[0]
    if backend.name == 'facebook':
        profile.photo  = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
    elif backend.name == "twitter":
        if response['profile_image_url'] != '':
            if not response.get('default_profile_image'):
                avatar_url = response.get('profile_image_url_https')
                if avatar_url:
                    avatar_url = avatar_url.replace('_normal.', '_bigger.')
                    profile.photo = avatar_url
    elif backend.name == "google-oauth2":
        if response['image'].get('url') is not None:
            profile.photo  = response['image'].get('url')


    profile.save()



回答2:


Ok, so I will post the solution I found just in case someone has the same problem. I am not sure if this is the best way to do it, but it works:

    if "facebook" in kwargs['backend'].redirect_uri:
        url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
    elif "twitter" in kwargs['backend'].redirect_uri:
        if response['profile_image_url'] != '':
            url = response['profile_image_url']
    elif "google" in kwargs['backend'].redirect_uri:
        if response['image'].get('url') is not None:
            url = response['image'].get('url')

Other solutions are welcome.



来源:https://stackoverflow.com/questions/25896165/attributeerror-djangostrategy-object-has-no-attribute-backend-python-social

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!