Python Social Auth 获取谷歌头像

标签 python google-plus google-signin python-social-auth

有没有办法使用 python 社交身份验证获取 Google 个人资料图片 url?

所以,这就是我为 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”吗? 其次,我应该使用什么 url 来保存个人资料的头像?。是这样吗?

最佳答案

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

关于Python Social Auth 获取谷歌头像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25734611/

相关文章:

python - 如何获取选中的项目QlistWidget pyqt

php - Google+ 分享片段未填充博客中的数据

python - ndb 中的游标如何工作?

java - 尝试获取 token 时出现 GoogleAuthException "Unknown"

android - revokeAccessAndDisconnect 和使用 Google+ 断开连接有什么区别?

javascript - Google 登录 API 挂起并出现 Uncaught Error 无法从 URL 哈希获取父源

ios - Firebase 更改显示在谷歌登录警报上的应用程序名称?

iOS Google Sign In SDK webview 签名后不关闭

python - 为什么这段代码不能无限运行?

python - 使用SSH中的SSH ProxyCommand的SFTP