【问题标题】:Deprecated offline_access on facebook with RoR使用 RoR 在 facebook 上弃用了 offline_access
【发布时间】:2012-03-02 03:33:50
【问题描述】:

我们的 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" }
}

提前致谢。

【问题讨论】:

    标签: ruby-on-rails-3 facebook facebook-graph-api omniauth koala


    【解决方案1】:

    这个问题有两种解决方案:

    • 扩展用户的访问令牌:
      • 根据this article on the Facebook docs,您可以请求将用户的访问令牌延长 60 天。但是,如果用户在该期限内没有返回,则此方法对您没有帮助。
      • 您可以找到一个 PHP 代码 sn-p 来执行此操作 at this StackOverflow question
        1. 为此,请向此 API 端点发送帖子:https://graph.facebook.com/oauth/access_token?client_id=APP_ID&amp;client_secret=APP_SECRET&amp;grant_type=fb_exchange_token&amp;fb_exchange_token=EXISTING_ACCESS_TOKEN

    • 捕获OAuthException 并请求新的访问令牌:
      • Facebook 提供了一个 PHP 代码 sn-p 概述此解决方案 on their dev blog
      • 基本上,您遵循以下步骤:
        1. 使用用户当前的access_token 调用图表。
        2. 如果调用成功,access_token 就可以了。如果它抛出OAuthException,将用户重定向到https://www.facebook.com/dialog/oauth?client_id=APP_ID&amp;redirect_uri=CALLBACK_URL
        3. 用户将被发送到该 URL,然后在参数中使用 code 重定向到您的 CALLBACK_URL
        4. 使用code向以下URL发送帖子以获得新的access_tokenhttps://graph.facebook.com/oauth/access_token?client_id=APP_ID&amp;redirect_uri=CALLBACK_URL&amp;client_secret=APP_SECRET&amp;code=CODE&amp;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
    

    考拉的叫声均取自以下 2 个教程:

    【讨论】:

    • 我已经阅读了该文档,但我需要针对 RoR 而不是 PHP 的解决方案。
    • 非常感谢,它可以工作了! :)
    【解决方案2】:

    对于那些没有时间进行此更改的人,我发现您可以在“设置”->“高级”中禁用此迁移。选项名称为“移除offline_access权限:”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      相关资源
      最近更新 更多