【问题标题】:AttributeError: 'DjangoStrategy' object has no attribute 'backend' python social authAttributeError: 'DjangoStrategy' 对象没有属性'backend' python social auth
【发布时间】:2014-11-11 19:40:49
【问题描述】:

上周有效。也许是我做错了什么并在其他地方搞砸了,或者它是一个错误,或者它只是一个更新,我在阅读文档时错过了它。

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

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

【问题讨论】:

  • 你项目中的 DjangoStrategy 是什么?是模特吗?
  • 没有。 Django Strategy 是 Python Social Auth 使用的对象。
  • 您是否将python-social-auth 更新到版本0.2.2?如果是这种情况,那么您需要更新您的管道,只需删除 strategy.backend 用法,并将 backend 参数添加到函数并使用该参数。
  • 不,我使用的是 python-social-auth 0.2.1。这很奇怪,因为昨天我做了pip-review --auto 更新了所有的包。无论如何,我使用kwargs['backend'].redirect_uri 解决了这个问题并在那里检查后端名称。我不确定这是否是最好的方法,但它确实有效。
  • 我的评论仍然适用,backend 参数是在版本0.2.0 中添加的。

标签: python django python-social-auth


【解决方案1】:

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

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

欢迎使用其他解决方案。

【讨论】:

    【解决方案2】:

    其他解决方案:

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

    【讨论】:

    • 嗨。这与我所拥有的相似。我很好奇,你在哪里定义“后端”?这是否适用于最新版本的 python social auth?
    • 太棒了,我正在关注使用 strategystrategy.backend 而不仅仅是 backend 参数的代码示例。这在现代 Python Social Auth 中效果更好。
    猜你喜欢
    • 2016-12-17
    • 2022-12-15
    • 2021-06-18
    • 2020-03-29
    • 2017-07-14
    • 2011-08-27
    • 2021-08-20
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多