我们的RoR应用中存在问题。我们正在使用omniauth的facebook身份验证,并使用Koala搜索用户朋友。但是最近,当我们试图展示朋友的照片时,我们收到了这个错误:
Koala::Facebook::APIError in Homes#show
Showing /home/daniel/Homes/app/views/shared/_event.html.erb where line #19 raised:
OAuthException: Error validating access token: Session has expired at unix time 1328727600. The current unix time is 1328802133.
Extracted source (around line #19):
16: <img src="../assets/friends-icon.png" alt="User profile apicture" height="33" width="43">
17: <% if current_user %>
18: <% event.friends_in_event(@person).each do |f| %>
19: <%= link_to(image_tag(f.fb_picture, :size => "43x33"), person_path(f.id)) %>
20: <% end %>
21: <% end %>
22: </div>
身份验证效果很好,但facebook已经弃用了offline_access选项,这项工作正常,但现在我们遇到了这个问题。 是扩展access_token?的任何方式,还是有另一种解决方案?。
这是我们的omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FB_KEY'], ENV['FB_SECRET'],
{ :scope => 'email,offline_access,user_photos,publish_stream',
:client_options => { :ssl => { :ca_path => "/etc/ssl/certs" } } }
end
我们的koala.rb
Koala.http_service.http_options = {
:ssl => { :ca_path => "/etc/ssl/certs" }
}
提前致谢。
答案 0 :(得分:9)
这个问题有两个解决方案:
https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=fb_exchange_token&fb_exchange_token=EXISTING_ACCESS_TOKEN
OAuthException
并请求新的访问令牌
access_token
。access_token
就可以了。如果它引发OAuthException
,则会将用户重定向到https://www.facebook.com/dialog/oauth?client_id=APP_ID&redirect_uri=CALLBACK_URL
CALLBACK_URL
参数中的code
。code
发送帖子到以下网址,以获取新的access_token
:https://graph.facebook.com/oauth/access_token?client_id=APP_ID&redirect_uri=CALLBACK_URL&client_secret=APP_SECRET&code=CODE&display=popup
阅读他们的开发博客上的帖子以获取更多信息。
编辑(添加示例Ruby on Rails代码):
将以下内容添加到ApplicationController
的顶部:
rescue_from Koala::Facebook::APIError, :with => :handle_fb_exception
将以下protected
方法添加到ApplicationController
:
def handle_fb_exception exception
if exception.fb_error_type.eql? 'OAuthException'
logger.debug "[OAuthException] Either the user's access token has expired, they've logged out of Facebook, deauthorized the app, or changed their password"
oauth = Koala::Facebook::OAuth.new
# If there is a code in the url, attempt to request a new access token with it
if params.has_key? 'code'
code = params['code']
logger.debug "We have the following code in the url: #{code}"
logger.debug "Attempting to fetch a new access token..."
token_hash = oauth.get_access_token_info code
logger.debug "Obtained the following hash for the new access token:"
logger.debug token_hash.to_yaml
redirect_to root_path
else # Since there is no code in the url, redirect the user to the Facebook auth page for the app
oauth_url = oauth.url_for_oauth_code :permissions => 'email'
logger.debug "No code was present; redirecting to the following url to obtain one: #{oauth_url}"
redirect_to oauth_url
end
else
logger.debug "Since the error type is not an 'OAuthException', this is likely a bug in the Koala gem; reraising the exception..."
raise exception
end
end
Koala调用全部取自以下2个教程:
答案 1 :(得分:2)
对于那些没有时间进行此更改的人,我发现您可以在“设置”中禁用此迁移 - &gt;高级。该选项的名称是“Remove offline_access permission:”