ruby-on-rails - 使用Koala和omniauth-facebook的Facebook token 到期和续订

标签 ruby-on-rails omniauth koala

我正在编写一个使用omniauth-facebook来针对FB验证用户身份(并为该用户获取FB OAuth访问 token )的Rails应用。然后,该应用使用已保存的OAuth token ,使用Koala对FB Graph API进行各种调用。

每当用户重新进行身份验证时(通常在他们登录到我的应用程序时),我都会更新保存的 token 。即使这样,保存的 token 仍会不时过期(或变得无效)。

在使用Koala时防止身份验证失败和更新 token 的最佳实践是什么?

是否应该将所有调用都包装在begin / rescue块中,并使用异常处理程序根据FB重新对用户进行身份验证?

是否有某种方法(使用Koala)来利用here描述的“扩展访问 token ”过程?如果不是,是否存在编写自己的代码以从Koala调用中自己提取新 token 的最佳实践?

最佳答案

我所拥有的是一个before_filter,它在需要 Activity Facebook session 的每个页面上触发。这样的事情应该起作用:

  before_filter :reconnect_with_facebook
  def reconnect_with_facebook
    if current_account && current_account.token_expired?(session[:fb]["expires"])

    # re-request a token from facebook. Assume that we got a new token so
    # update it anyhow...
    session[:return_to] = request.env["REQUEST_URI"] unless request.env["REQUEST_URI"] == facebook_request_path
    redirect_to(with_canvas(facebook_request_path)) and return false
  end
end

token_expired?方法看起来像这样:
def token_expired?(new_time = nil)
  expiry = (new_time.nil? ? token_expires_at : Time.at(new_time))
  return true if expiry < Time.now ## expired token, so we should quickly return
  token_expires_at = expiry
  save if changed?
  false # token not expired. :D
end

关于ruby-on-rails - 使用Koala和omniauth-facebook的Facebook token 到期和续订,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10249778/

相关文章:

ruby-on-rails - 从 Page Feed 获取大图像?使用 Koala Gem & Rails 3.2

ruby-on-rails-3 - 在 Rails 3 应用程序中使用 Koala 和 Devise & OmniAuth 获取 SSL 错误

ruby-on-rails - rails : Issues with getting Bootstrap Typeahead to work

ruby-on-rails - 创建一个无数据库的 rails 模型,作为在 elasticsearch 中索引数据的 View

ruby-on-rails - 如何处理 'record does not exist'错误并防止后台作业尝试重试?

ruby-on-rails - 使用 OAuth 连接到 Linkedin 时,是否可以获取用户的非主要电子邮件地址?

ruby-on-rails - 哪些 Ruby gem 支持 Facebook API?

jquery - Rails ajax 前置事件

ruby-on-rails-3 - Rails omniauth - 每次用户登录时,twitter 都会要求应用程序授权

facebook - 如何记录 Koala API 的请求和回复?