python - 在 python-social-auth 中从 google 和 facebook 检索个人资料图片

标签 python django python-2.7 python-social-auth

如何通过扩展管道使用 python-social-auth 从 google 和 facebook 检索个人资料图片和出生日期?我读过我可以创建函数来执行此操作并为它们设置路径,但我不知道我必须检索的属性名称。请帮忙!

最佳答案

要从社交登录获取头像,您需要在您的应用中创建一个 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 the 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 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()

关于python - 在 python-social-auth 中从 google 和 facebook 检索个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29575713/

相关文章:

Python if 语句不起作用?

python - spacy-udpipe 与 pytextrank 从非英语文本中提取关键字

python - 使用 python 程序(理想情况下)将 Ubuntu 源中的 mp3 文件流式传输到 Raspberry Pi

python - 更新多对多关系

python - 循环工作时间过长

python - Django 按月和年分组不起作用

python - 在 Django 中从外部 Active Directory 提取数据

python - 如果函数在匹配来自不同列表的两个用户 ID 时执行函数?

python - 使用 self.sender() 检查 PyQt QPushButton 是否被检查

python-2.7 - 使用 python 和 pyshark 跟踪 TCP 流