Search code examples
ruby-on-railsoauth-2.0facebook-access-token

how to get oauth access token for facebook using ruby


Am trying to get oauth access token for facebook programmatically in ruby.

My code is as follows:

client = OAuth2::Client.new(
  APP_ID,
  SECRET_ID,
  :authorize_url => "/dialog/oauth",
  :token_url => "/oauth/access_token",
  :site => "https://www.facebook.com/"
)

code = client.auth_code.authorize_url(:redirect_uri => "http://www.facebook.com/")
token = client.auth_code.get_token(code, :redirect_uri => "https://graph.facebook.com/")
OAuth2::AccessToken.new(client, token.token, {:mode => :query, :param_name =>"oauth_token"})

When i try to run the above ruby code, i'm getting the following exception

https://www.facebook.com/dialog/oauth?response_type=code&client_id=APP_ID
51&redirect_uri=http%3A%2F%2Fwww.facebook.com%2F

/home/ec2-user/.rvm/gems/ruby-1.9.3-p0@samples/gems/oauth2-0.5.2/lib/oauth2/clie
nt.rb:129:in `get_token': OAuth2::Error (OAuth2::Error)
        from /home/ec2-user/.rvm/gems/ruby-1.9.3-p0@samples/gems/oauth2-0.5.2/li
b/oauth2/strategy/auth_code.rb:29:in `get_token'
        from oauth.rb:16:in `<main>'

Any help is greatly appreciated as I have spent more than a day while trying to sort this out.


Solution

  • Have you tried to put as redirect_uri instead of localhost:3000 your real IP address ex. 231.61.233.57:3000? Additionally you could try to use ssh tunneling for testing purposes so your localhost application will be available worldwide. Check this out http://progrium.com/localtunnel/ . When you will get ip address from this tool try to set redirect_uri param to it.