How can I get the 'data' info that was sent along with Facebook Request
I user invites friends to my application using a request dialogue.
I send along extra data as follows:
FB.ui({
method: 'apprequests',
message: "Come to this great site,
data: '{"page_id": "<%=@page.id%>"}'
},...
When the request is accepted by the person invited they are taken to my site.
I want to read the info I sent along in the 开发者_如何学JAVA'data'.I understand I need to get at the request object_ids I have those.
I just don't know how to get at the information inside the object in Rails and Javascript.
I saw this example using php:
$request_content = json_decode(file_get_contents("https://graph.facebook.com/$request_id?$app_token"), TRUE);
extract(json_decode($request_content['data'], TRUE));
How can I do this using Rails, Ruby from inside my controller?
I am using OmniAuth BTW
I strongly discourage you from attempting to implement that in Javascript as it would allow users to find your App access token in their browser, which is a massive security issue for your app.
Server-side, you can do it in any basically language (and could be triggered by a Javascript call to your backed from the user's browser), the PHP example should be applicable to any language which has the ability to decode JSON.
In rails you need to use facebook api wrapper like Koala, facebooker 2 etc and you can add it easily. Omniauth only provides Authentication and nothing more. You can save token from omniauth facebook authentication and use it to request user for extra information with koala.
I am a mega NOOB but this works for me so I thought I would share it:
The user clicks 'allow' in facebook:
1. User sent to authorize action in a controller to store the request ids
2. They are sent to a page that has a javascript redirect to the sessions controller action
In the sessions controller:
require "net/https"
require "net/http"
require "uri"
request_id = session[:request_ids].last
auth = request.env["omniauth.auth"]
access_token = "access_token=#{auth['credentials']['token']}"
request_url = URI.parse("https://graph.facebook.com/#{request_id}?#{URI.escape(access_token)}")
http = Net::HTTP.new(request_url.host, request_url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request_body = http.request(Net::HTTP::Get.new(request_url.request_uri))
request_content = ActiveSupport::JSON.decode(request_body.body)
The request_content will contain all the information sent with the original application requests.
So you can access the objects as you would any hash.
request_content["data"] # to get the data sent along.
Again take this with a grain of salt. It works but it might not be the best solution.
精彩评论