使用 RoR 弃用了 facebook 上的offline_access

发布于 2025-01-03 16:43:35 字数 1372 浏览 6 评论 0原文

我们的 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 已弃用离线访问选项,该选项工作正常,但现在我们遇到了这个问题。 有什么方法可以扩展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" }
}

提前致谢。

We have a problem in our RoR app. We are using a facebook authentication with omniauth, and searching the user friends with Koala. But lately, when we try to show a friend photo, we got this error:

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>

The authentication works good, but facebook has already deprecated the offline_access option, that was working good, but now, we have this issue.
is It any way to extends the access_token?, or are there another solution?.

This is our 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

And our koala.rb

Koala.http_service.http_options = {
  :ssl => { :ca_path => "/etc/ssl/certs" }
}

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无敌元气妹 2025-01-10 16:43:35

此问题有 2 个解决方案:

  • 扩展用户的访问令牌:
    • 根据Facebook 文档中的这篇文章,您可以请求用户访问令牌的 60 天延期。但是,如果用户没有在该期限内返回,则此方法将无济于事。
    • 您可以在此 StackOverflow 问题中找到执行此操作的 PHP 代码段。
      1. 为此,请向此 API 端点发送帖子: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 并请求新的访问权限令牌:
    • Facebook 在其开发博客上提供了一个 PHP 代码片段,概述了此解决方案。
    • 基本上,您可以按照以下步骤操作:
      1. 使用用户当前的 access_token 调用图表。
      2. 如果调用成功,则 access_token 就可以了。如果抛出 OAuthException,则将用户重定向到 https://www.facebook.com/dialog/oauth?client_id=APP_ID&redirect_uri=CALLBACK_URL
      3. 用户将被发送到该网址,然后重定向到您的 CALLBACK_URL,并在参数中包含代码
      4. 使用 code 向以下网址发送帖子以获取新的 access_tokenhttps://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 个教程:

There are 2 solutions to this problem:

  • Extend the user's access token:
    • As per this article on the Facebook docs, you may request a 60-day extension on a user's access token. However, if the user does not return within that period, this method won't help you.
    • You can find a PHP code snippet to do this at this StackOverflow question.
      1. To do this, send a post to this API endpoint: 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

  • Catch the OAuthException and request a new access token:
    • Facebook provides a PHP code snippet outlining this solution on their dev blog.
    • Basically, you follow these steps:
      1. Make a call to the graph with the user's current access_token.
      2. If the call succeeds, the access_token is fine. If it throws an OAuthException, redirect the user to https://www.facebook.com/dialog/oauth?client_id=APP_ID&redirect_uri=CALLBACK_URL
      3. The user will be sent to that URL and then redirected to your CALLBACK_URL with a code in the parameters.
      4. Send a post to the following URL with the code to obtain a new 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

Read the post on their dev blog for more information.

Edit (adding example Ruby on Rails code):

Add the following to the top of your ApplicationController:

rescue_from Koala::Facebook::APIError, :with => :handle_fb_exception

Add the following protected method to your 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

The Koala calls were all taken from the following 2 tutorials:

喵星人汪星人 2025-01-10 16:43:35

对于那些没有时间进行此更改的人,我发现您可以在“设置”->“设置”中禁用此迁移。先进的。该选项的名称是“删除离线访问权限:”

For those of you who don't have time to make this change, I found that you can disable this migration in Settings -> Advanced. The name of the option is "Remove offline_access permission:"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文