Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I ran into the same problem when trying to use facebook_oauth (https://github.com/moomerman/facebook_oauth) on my rails app. After spending an hour or so trying to change its code, i realized it might be easier just to use oauth2 directly. I solved the problem and now there's no need for that intermediate library. Here's how:</p> <p>In <code>Gemfile</code> add</p> <pre><code>gem 'oauth2' </code></pre> <p>Then run</p> <p><code>bundle update</code></p> <p>Then, in your <code>login_via_facebook</code> method you either construct the dialog uri manually or use the oauth client something along these lines:</p> <pre><code>oauth_client = OAuth2::Client.new(APPLICATION_ID, APPLICATION_SECRET, { :authorize_url =&gt; 'https://www.facebook.com/dialog/oauth' }) redirect_to oauth_client.authorize_url({ :client_id =&gt; APPLICATION_ID, :redirect_uri =&gt; YOUR_REDIRECT_URL }) </code></pre> <p>If you need to request additional permissions, specify <code>scope</code> param in the <code>authorize_url</code> call:</p> <pre><code>redirect_to oauth_client.authorize_url({ :client_id =&gt; APPLICATION_ID, :redirect_uri =&gt; YOUR_REDIRECT_URL, :scope =&gt; 'offline_access,email' }) </code></pre> <p>Then, in the method that handles YOUR_REDIRECT_URL (i call mine <code>login_via_facebook_callback</code>), do something like this:</p> <pre><code>oauth_client = OAuth2::Client.new(APPLICATION_ID, APPLICATION_SECRET, { :site =&gt; 'https://graph.facebook.com', :token_url =&gt; '/oauth/access_token' }) begin access_token = oauth_client.get_token({ :client_id =&gt; APPLICATION_ID, :client_secret =&gt; APPLICATION_SECRET, :redirect_uri =&gt; YOUR_REDIRECT_URL, :code =&gt; params[:code], :parse =&gt; :query }) access_token.options[:mode] = :query access_token.options[:param_name] = :access_token facebook_user_info = access_token.get('/me', {:parse =&gt; :json}).parsed rescue Error =&gt; e # You will need this error during development to make progress :) #logger.error(e) end </code></pre> <p>Now <code>facebook_user_info</code> has the basic user info!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload