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

Ale*_*lla 4 python django python-social-auth

这上周工作了.也许我做错了什么并搞砸了别的地方,或者它可能是一个bug,或者它只是一个更新而我在阅读文档时错过了它.

我有一个管道,可以获取用户的头像并保存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')
Run Code Online (Sandbox Code Playgroud)

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

 'DjangoStrategy' object has no attribute 'backend'
Run Code Online (Sandbox Code Playgroud)

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

小智 5

其他方案:

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()
Run Code Online (Sandbox Code Playgroud)


Ale*_*lla 0

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

    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')
Run Code Online (Sandbox Code Playgroud)

欢迎其他解决方案。