views:

480

answers:

1

Hi,

I'm trying to implement single sign-on using facebook in my ruby sinatra app. So far, I've been following this tutorial:

http://jaywiggins.com/2010/05/facebook-oauth-with-sinatra/

I am able to send a request for a user to connect to my application but I'm having trouble actually "getting" the access token. The user can connect without trouble and I receive a response with the "code" parameter, which I'm supposed to use to exchange an Access Token - but its here where I get stuck.

So I submit a url with the following parameters:

https://graph.facebook.com/oauth/access_token/{client_id}&{client_secret}&{code}&{redirect_uri}

The words in the curly brackets above are obviously replaced by the values.

I submit this using the following code:

response = open(url)

This doesn't seem to return anything of use in the way of an access token (it has a @base_uri which is the url I submitted above and few other parameters, though nothing useful looking). However, if I take that url I submitted and paste it into a browser, I receive back an access token.

Can anyone tell me how I can get the request back from facebook and pull out the access token?

Thanks.

A: 

assuming you are using open-uri, you should be getting a file handle back with some extensions.

That means you can get the body of the response by reading it like any file handle.

body = response.read

Then you probably want to parse the json and pull out the access token:

require 'json'
JSON.parse(body)['access_token']
BaroqueBobcat