Facebook令牌到期和更新,与考拉和omniauth-facebook

时间:2012-04-20 16:14:33

标签: ruby-on-rails omniauth koala

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

每次用户重新进行身份验证时(通常在用户登录我的应用时),我都会更新已保存的令牌。即便如此,保存的令牌也会不时到期(或以其他方式变为无效)。

在使用考拉时,防止auth失败和更新令牌的最佳做法是什么?

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

是否有某种方式(使用Koala)利用here所述的“扩展访问令牌”流程?如果没有,是否有编写我自己的代码以从Koala调用中自己提取新令牌的最佳实践?

3 个答案:

答案 0 :(得分:17)

我所拥有的是在需要有效Facebook会话的每个页面上触发的before_filter。这样的事情应该有效:

  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

答案 1 :(得分:17)

我遇到this post来调整Railscast on Facebook中的代码,以显示如何为60天代码交换短期代币:

<强> user.rb

 def self.from_omniauth(auth)

    # immediately get 60 day auth token
    oauth = Koala::Facebook::OAuth.new(ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"])
    new_access_info = oauth.exchange_access_token_info auth.credentials.token

    new_access_token = new_access_info["access_token"]
    # Facebook updated expired attribute
    new_access_expires_at = DateTime.now + new_access_info["expires_in"].to_i.seconds

    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.image = auth.info.image
      user.email = auth.info.email
      user.oauth_token = new_access_token #originally auth.credentials.token
      user.oauth_expires_at = new_access_expires_at #originally Time.at(auth.credentials.expires_at)
      user.save!
    end
  end

答案 2 :(得分:0)

您可以执行以下操作,检查access_token是否已过期并生成另一个。

 %w[facebook].each do |provider|
   scope provider, -> { where(provider: provider) }
 end

 def client
   send("#{provider}_client")
 end

 def expired?
   expires_at? && expires_at <= Time.zone.now
 end

 def access_token
   send("#{provider}_refresh_token!", super) if expired?
   super
 end

 def facebook_refresh_token!(token)
   new_token_info = 
   Koala::Facebook::OAuth.new.exchange_access_token_info(token)
   update(access_token: new_token_info["access_token"], expires_at: Time.zone.now + new_token_info["expires_in"])
 end

您可以查看gorails screencast,深入解释此问题。