2012-02-09 12 views
5

Abbiamo un problema nella nostra app RoR. Utilizziamo un'autenticazione di Facebook con omniauth e cerchiamo gli amici degli utenti con Koala. Ma ultimamente, quando cerchiamo di mostrare un amico foto, abbiamo ottenuto questo errore:Offline_access deprecato su Facebook con RoR

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> 

L'autenticazione funziona bene, ma Facebook ha già deprecato l'opzione offline_access, che stava lavorando bene, ma ora, abbiamo questo problema . È un modo per estendere access_token? O ci sono altre soluzioni ?.

Questo è il nostro 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 

E la nostra koala.rb

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

Grazie in anticipo.

risposta

9

Ci sono 2 soluzioni a questo problema:

  • estendere token di accesso dell'utente:
    • Come da this article on the Facebook docs, è possibile richiedere una proroga di 60 giorni sui token di accesso di un utente. Tuttavia, se l'utente non ritorna entro quel periodo, questo metodo non ti aiuterà.
    • È possibile trovare uno snippet di codice PHP per fare questo at this StackOverflow question.
      1. Per fare questo, inviare un post a questa 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 e richiedere un nuovo token di accesso:
    • Facebook fornisce uno snippet di codice PHP che illustra questa soluzione on their dev blog.
    • In sostanza, si attenersi alla seguente procedura:
      1. Effettuare una chiamata al grafico con l'attuale access_token dell'utente.
      2. Se la chiamata riesce, access_token va bene. Se getta un, reindirizza l'utente a https://www.facebook.com/dialog/oauth?client_id=APP_ID&redirect_uri=CALLBACK_URL
      3. L'utente verrà inviato a tale URL e quindi reindirizzato al tuo CALLBACK_URL con un code nei parametri.
      4. Invia un messaggio al seguente URL con il code per ottenere un nuovo 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

leggere il post sul loro blog dev per ulteriori informazioni.

Edit (ad esempio l'aggiunta di Ruby on Rails codice):

Aggiungere il seguente alla parte superiore del vostro ApplicationController:

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

Aggiungere il protected seguente metodo per il vostro 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 

Le chiamate Koala sono state tutte prese dai seguenti 2 tut orials:

+0

Ho già letto quella documentazione, ma ho bisogno di una soluzione per RoR non PHP. – Dagosi

+1

Ho aggiunto qualche esempio di codice Rails per iniziare. –

+0

Grazie mille, FUNZIONA! :) – Dagosi

2

Per quelli di voi che non hanno il tempo di fare questo cambiamento, ho trovato che è possibile disattivare questa migrazione in Impostazioni -> Avanzate. Il nome dell'opzione è "Rimuovi autorizzazione offline_access:"

Problemi correlati