【问题标题】:Allauth Facebook profile image not loadingAllauth Facebook 个人资料图片未加载
【发布时间】:2014-02-12 16:23:26
【问题描述】:

我知道这是一个常见问题,所以请原谅我再次询问(这真的不是缺乏研究),我似乎无法找到使用 Allauth 加载 Facebook 个人资料图片的方法。

我使用http://www.sarahhagstrom.com/2013/09/the-missing-django-allauth-tutorial/ 获得了一些提示,它们都运行良好,但是对于 Facebook 个人资料图片似乎缺少一些东西,models.py 上负责传递 Facebook 个人资料图片 URL 的代码无法获取 Facebook 用户身份证

在下面的代码中...

      http://graph.facebook.com/{}/picture?width=40&height=40

...我尝试使用 fb_uid 和 user_id 但仍然无法加载。

模板上的配置文件 URL 被调用为:

      {% if request.user.is_authenticated %}
             <img src="{{ request.user.profile.profile_image_url }}"/>
      {% endif %}

Pennersr 在此处建议的 How can I get the user's facebook id with django-allauth? 也不允许我访问用户 ID。

谢谢

【问题讨论】:

    标签: python django facebook django-allauth


    【解决方案1】:

    facebook api 不允许直接访问图像,您必须编写代码将响应从 facebook 个人资料 url 下载到您的服务器并使用您的图像副本。

    上面的代码为你提供了 facebook 重定向到他们存储的 URL,所以使用

    url = "http://graph.facebook.com/%s/picture?type=large" % response['id']
    avatar = urlopen(url)
    profile = user.get_profile()
    profile.profile_photo.save(slugify(user.username + " social") + '.jpg', 
                            ContentFile(avatar.read()))              
    profile.save()
    

    但不推荐这种方式,会导致响应延迟,有人推荐使用celery后台任务下载图片,也请研究一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-21
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      相关资源
      最近更新 更多