5

RoR アプリに問題があります。omn​​iauth で 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" }
}

前もって感謝します。

4

2 に答える 2

9

この問題には 2 つの解決策があります。

  • ユーザーのアクセス トークンを拡張します。
    • Facebook ドキュメントのこの記事に従って、ユーザーのアクセス トークンの 60 日間の延長をリクエストできます。ただし、ユーザーがその期間内に戻ってこない場合、この方法は役に立ちません。
    • この StackOverflow question で、これを行うための 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. ユーザーの current を使用してグラフを呼び出しますaccess_token
      2. 呼び出しが成功した場合は、access_token問題ありません。がスローされた場合はOAuthException、ユーザーをにリダイレクトしますhttps://www.facebook.com/dialog/oauth?client_id=APP_ID&redirect_uri=CALLBACK_URL
      3. ユーザーはその URL に送信され、パラメータCALLBACK_URLに acodeを指定してリダイレクトされます。
      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=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 つのチュートリアルから取得されました。

于 2012-02-09T18:41:08.170 に答える
2

この変更を行う時間がない方のために、[設定] -> [詳細設定] でこの移行を無効にできることがわかりました。オプションの名前は「offline_access 権限を削除する:」です。

于 2012-11-06T18:50:44.627 に答える