Stephen Stephen - 1 year ago 102
Ruby Question

What is Facebook callback_url and how to use it in rails?

I am using the

Facebook Graph API
in my rails projects, no matter I use
gem or
, It need


token = client.auth_code.get_token('code_value', :redirect_uri => 'http://localhost:8080/oauth/callback')


@oauth =, app_secret, callback_url)

I try to use
in my project, but it's not working.

Should I develop a routes for that?

get 'callback' => 'oauth#callback'

What should I write in the
method in
, what does it use for? Thanks

Answer Source

Yes, you should. Basically, OAuth uses callback data to provide tokens for authenticating users.

For example

  • user clicks on "sign in" (or whatever) link and your app redirects they to the OAuth provider (or open it in the iframe).
  • user permits to your app to use they profile details
  • OAuth provider send callback to your app with unique code
  • app uses that code to get secure access token for API communications

That's just a basic example.

In your case you need to implement controller that will parse callback data.

Here is the code example

@oauth =, app_secret, callback_url)
=> #<Koala::Facebook::OAuth:0x007fc919d014e0 @app_id=1234567890, @app_secret="FaKeAppSecretKey", @oauth_callback_url="http://localhost:3000/callback">

=> ""

And when you go to FB will redirect you to


Then you should use implement controller that uses code to get access token

access_token = @oauth.get_access_token(params[:code])

@graph =
 => #<Koala::Facebook::API:0x007fc91a903ae0  @access_token="ACCESS_TOKEN", @app_secret=nil>

profile = @graph.get_object("me")
=> {"id"=>"4492344324865", "email"=>"", "first_name"=>"Roman", "gender"=>"male", "last_name"=>"Sotnikov", "link"=>"", "locale"=>"en_US", "name"=>"Roman Sotnikov", "timezone"=>6, "updated_time"=>"2015-05-18T05:19:54+0000", "verified"=>true}

Please check for additional info.