Facebook令牌到期和更新,与考拉和omniauth-facebook
我正在写一个Rails应用程序,它使用omniauth-facebook来验证FB用户(并获得用户的FB OAuth访问令牌)。该应用程序然后使用Koala使用保存的OAuth令牌对FB Graph API进行各种调用。Facebook令牌到期和更新,与考拉和omniauth-facebook
每次用户重新进行身份验证时(通常是他们登录到我的应用程序时),我都会更新保存的令牌。即便如此,该保存的令牌将不时(或以其他方式无效)过期。
围绕使用考拉防止认证失败和更新令牌的最佳做法是什么?
是否应该将所有调用都封装在开始/救援块中,并使用异常处理程序对FB重新验证用户?
是否有某种方法(使用考拉)利用描述为here的“扩展访问令牌”过程?如果没有,是否有最佳做法来编写我自己的代码,以便从考拉呼叫中提取新的令牌?
我所拥有的是在需要活跃的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
我碰到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
感谢您的代码片段! – okysabeni 2014-05-18 20:36:19
谢谢,这是一个不错的方法。花了我一段时间来弄清楚为什么我的测试抱怨 '考拉:: Facebook :: OAuthTokenRequestError: 类型:OAuthException,代码:101,消息:缺少client_id参数。 [HTTP 400]'。最终,我意识到我没有在'secrets.yml'中为我的测试环境定义ENV [“FACEBOOK_APP_ID”]或ENV [“FACEBOOK_SECRET”]。 – DazBaldwin 2015-02-05 00:41:59
你可以做这样的事情,你检查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,这个深度解释这个。
感谢您的回答。 session [:fb] [“expires”]在哪里/如何设置?您可以在上面的reconnect_with_facebook方法中使用它。 – 2012-05-07 15:55:13
我设置了第一次有人成功登录创建会话。所有其他连接被视为“重新连接” – heavysixer 2012-05-07 18:54:32
谢谢。我的实施方式有所不同,但这有助于我走上正确的道路。 – 2012-05-16 18:30:17