簡體   English   中英

使用RoR在facebook上棄用了offline_access

[英]Deprecated offline_access on facebook with RoR

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

提前致謝。

這個問題有兩個解決方案:

  • 擴展用戶的訪問令牌:
    • 根據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_TOKENhttps://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. 用戶將被發送到該URL,然后使用參數中的code重定向到您的CALLBACK_URL
      4. 使用code發送帖子到以下URL以獲取新的access_tokenhttps://graph.facebook.com/oauth/access_token?client_id=APP_ID&redirect_uri=CALLBACK_URL&client_secret=APP_SECRET&code=CODE&display=popupaccess_token https://graph.facebook.com/oauth/access_token?client_id=APP_ID&redirect_uri=CALLBACK_URL&client_secret=APP_SECRET&code=CODE&display=popup 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 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 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 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 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 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 code https://graph.facebook.com/oauth/access_token?client_id=APP_ID&redirect_uri=CALLBACK_URL&client_secret=APP_SECRET&code=CODE&display=popup 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 access_token

閱讀他們的開發博客上的帖子了解更多信息。

編輯(添加示例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個教程:

對於那些沒有時間進行此更改的人,我發現您可以在“設置” - >“高級”中禁用此遷移。 該選項的名稱是“刪除offline_access權限:”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM