开发者

HTTP Get Request

开发者 https://www.devze.com 2022-12-29 08:04 出处:网络
I\'ve been trying to get Rails to play with the new Facebook Graph API. After I get the authorization \"code\", I need to send another request which returns the access token in JSON form.

I've been trying to get Rails to play with the new Facebook Graph API. After I get the authorization "code", I need to send another request which returns the access token in JSON form.

It seems to work fine, however I want to fetch the access token JSON without redirecting the user. I'm att开发者_运维技巧empting to use Net::HTTP.get, but I'm not sure how to use it to get a request body, or even if it's the right thing to use to begin with.

Can anyone give an example of performing an HTTP GET?


I've figured out how to do this, the problem was mainly with the fact that I needed an HTTPS connection.

Adapted from http://snippets.dzone.com/posts/show/788:

path = '/oauth/access_token?...'
http = Net::HTTP.new('graph.facebook.com', 443)
http.use_ssl = true
res = http.get(path, nil)
@access_token = res.body

Anyone specifically trying to use the Graph API, note that the value stored in @access_token is in the form of a params string, e.g. "access_token=xxxx&expires=1234".

I got around needing to parse this by just redirecting to another page and using that as the URL params, but there's probably a better way to do this.


SOA#1

However please note that it means that server have to be log onto facebook - while if browser is redirecting it is user who have to be log into server. Hence did your server set the permission?

  • You can pretend that you are the user. Bad Horrible idea (you have to store passwords in cleartext on you server).
  • You can use OAuth. Hence you should use OAuth gem instead of Net::HTTP. You will not avoid the redirection - it is part of authorisation process and user must say that he allows to access data (imagine what would be if anyone could access anyone data on facebook). Turorial on writing OAuth clients in rails.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号