English 中文(简体)
Getting the Access Token from a Facebook Open Graph response in Ruby
原标题:

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.

最佳回答

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 ]
问题回答

暂无回答




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签