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

时间:2014-09-17 16:51:08

标签: python django python-social-auth

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

我有一个管道,可以获取用户的头像并保存网址:

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'

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

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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')

欢迎其他解决方案。