【问题标题】:Getting FB profile photo with Shrine用 Shrine 获取 FB 头像
【发布时间】:2021-08-20 19:21:03
【问题描述】:

我在我的项目中使用 Devise 进行身份验证,并且在使用 Shrine gem 通过 FB 登录时无法获取图像。

我有标准设计方法从用户模型中的 Omniauth 提供者获取数据:

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0, 20]
      # Standard version
      user.avatar = auth.info.image
      # For Google:
      # user.avatar_remote_url = auth.info.image
    end
  end

注释行适用于使用 Google 登录并将图像正确设置为 auth.info.image 返回链接,当粘贴到浏览器中时,会在浏览器中打开图像。然而,对于 FB,这行代码会导致无法持久化用户 - 如果 FB auth.info.image 返回一个链接,如果将文件粘贴到浏览器中,该链接会将文件直接下载到 HDD。

另一方面,标准版本在尝试注册时会导致错误809: unexpected token at 'http://graph.facebook.com/v4.0/xxxxxxxxxxx/picture?access_token=xxxxxxxx

在另一个项目中,我成功地以标准方式从 fb 获取图像,但我使用的是回形针。我肯定为 Shrine 设置了正确的设置,并且为数据准备了用户(仔细检查了它,而且 - 它适用于带有 _remote_url 帮助器的 Google oauth) - 我不确定如何正确处理从 FB 接收到的数据与神社。有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby devise shrine


    【解决方案1】:

    https://github.com/simi/omniauth-facebook#configuring 下,它声明提供程序的默认配置是secure_image_url: true,但似乎并非如此。答案是将secure_image_url: true 添加到我的Facebook 提供者哈希中,然后它返回https 链接而不是通过auth.info.image 分配给user.avatar_remote_url 的http。问题可以结束了。

    【讨论】:

      猜你喜欢
      • 2011-10-03
      • 2018-07-07
      • 2020-01-20
      • 2019-05-23
      • 1970-01-01
      • 2018-04-25
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多