facebook的Facebook令牌到期和续订

facebook的Facebook令牌到期和续订

本文介绍了使用Koala和omniauth-facebook的Facebook令牌到期和续订的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a Rails app that uses omniauth-facebook to authenticate the user against FB (and to get a FB OAuth access token for the user). The app then uses Koala to make various calls to the FB Graph API, using that saved OAuth token.

每次用户重新验证时(通常是他们登录到我的应用程序时),我都会更新保存的令牌.即使这样,保存的令牌仍会不时过期(或变得无效).

I update the saved token each time the user re-authenticates (typically when they log in to my app). Even so, that saved token will expire (or otherwise become invalid) from time to time.

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

What's the best practice around guarding against auth failures and updating the token while using Koala?

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

Should all calls be wrapped in begin/rescue blocks, with an exception handler that re-authenticates the user against FB?

是否有某种方式(使用Koala)来利用这里?如果不是,是否有最佳实践来编写我自己的代码以从Koala调用中提取我自己的新令牌?

Is there some way (using Koala) to take advantage of the 'extending access tokens' process described here? If not, are there best practices on writing my own code to extract the new token myself from a Koala call?

推荐答案

我所拥有的是一个pre_filter,它在需要活动Facebook会话的每个页面上触发.这样的事情应该起作用:

What I have is a before_filter that is triggered on every page that requires an active Facebook session. Something like this should work:

  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?方法如下:

The token_expired? method looks like this:

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

这篇关于使用Koala和omniauth-facebook的Facebook令牌到期和续订的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:08