【问题标题】:Python Social Auth get Google avatarPython Social Auth 获取谷歌头像
【发布时间】:2014-11-02 06:48:44
【问题描述】:

有没有办法使用 python social auth 获取 Google 个人资料图片网址?

所以,这就是我为 facebook 和 twitter 做的,用这段代码添加一个管道:

    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 == "GoogleOAuth2":  # doesn't work
        user_id = response['id']
        url = "???"

首先,我不知道后端的名称是什么,是“GoogleOAuth2”吗? 其次,我应该使用什么网址来保存个人资料的头像?是这样吗?

【问题讨论】:

    标签: python google-plus google-login python-social-auth


    【解决方案1】:
    from social.backends.google import GoogleOAuth2
    
    def save_profile(backend, user, response, *args, **kwargs):
        if isinstance(backend, GoogleOAuth2):
            if response.get('image') and response['image'].get('url'):
                url = response['image'].get('url')
                ext = url.split('.')[-1]
                user.avatar.save(
                   '{0}.{1}'.format('avatar', ext),
                   ContentFile(urllib2.urlopen(url).read()),
                   save=False
                )
                user.save()
    

    【讨论】:

    • 感谢 vero4ka。我不想保存图像,只有 url 所以url_50 = response['image'].get('url') 成功了。希望google不要很快改变url格式。
    【解决方案2】:

    要从社交登录中获取头像,您需要在您的应用中创建一个 pipeline.py 文件并将此行添加到 settings.py:

    SOCIAL_AUTH_PIPELINE = (
    
        'social.pipeline.social_auth.social_details',
        'social.pipeline.social_auth.social_uid',
        'social.pipeline.social_auth.auth_allowed',
        'social.pipeline.social_auth.social_user',
        'social.pipeline.user.get_username',
        'social.pipeline.user.create_user',
        'social.pipeline.social_auth.associate_user',
        'social.pipeline.social_auth.load_extra_data',
        'social.pipeline.user.user_details',
        'apps.users.pipeline.get_avatar', # This is a path of your pipeline.py
        #and get_avatar is the function.
    )
    

    稍后将此内容添加到您的 pipeline.py 文件中

    def get_avatar(backend, strategy, details, response,
            user=None, *args, **kwargs):
        url = None
        if backend.name == 'facebook':
            url = "http://graph.facebook.com/%s/picture?type=large"%response['id']
            # if you need a square picture from fb, this line help you
            url = "http://graph.facebook.com/%s/picture?width=150&height=150"%response['id']
        if backend.name == 'twitter':
            url = response.get('profile_image_url', '').replace('_normal','')
        if backend.name == 'google-oauth2':
            url = response['image'].get('url')
            ext = url.split('.')[-1]
        if url:
            user.avatar = url
            user.save()
    

    【讨论】:

    【解决方案3】:

    我不熟悉 Python 社交身份验证,但您似乎想要 GooglePlusAuth

    要获取图像 url,您必须向 people.get API method 发出 HTTP 请求,并将 userId 设置为 me 并以用户身份进行身份验证。响应包含一个 image.url 值。

    【讨论】:

      【解决方案4】:

      我正在使用 vero4ka 建议的方法。

      ext = url.split('.')[-1] 周围进入python 调试(import pdb; pdb.set_trace()),我注意到拆分输出并没有完全分离文件扩展名,即:

      (Pdb) url = response['image'].get('url').split('.')
      (Pdb) url
      [u'https://lh4', u'googleusercontent', u'com/redacted/redacted/photo', u'jpg?sz=50']
      

      我还需要在 ? 符号上分开。没什么大不了的。

      不知道为什么要以这种方式拆分然后重新组合扩展?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-03
        • 1970-01-01
        • 2019-10-19
        • 1970-01-01
        • 2012-02-21
        • 2014-08-04
        • 2014-12-22
        • 1970-01-01
        相关资源
        最近更新 更多