AttributeError: 'DjangoStrategy' 对象没有属性 'backend' python social auth



这上周起了作用。也许我做错了什么,把它搞砸了,也许这是一个错误,也许这只是一个更新,我在阅读文档时错过了它。

我有一个获取用户头像并保存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')

它曾经工作过,现在,它给了我一个错误:

 'DjangoStrategy' object has no attribute 'backend'

请帮忙,一些测试版用户已经在使用我的网站,目前他们没有个人资料图片。

其他解决方案:

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

好的,所以我会发布我找到的解决方案,以防有人遇到同样的问题。我不确定这是否是最好的方法,但它有效:

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

欢迎其他解决方案。

最新更新