Captured signed_request on Facebook app with Ruby / Sinatra and Rest-Graph stone

I created a Facebook application using Sinatra and a save-time stone . Now I would like to embed the application as an iframe bookmark on a Facebook page .

To do this, I need to get the data from signed_request sent to my Facebook application.

The Rest-Graph Pearl contains the following function on the Github page :

Utility to extract access_token and check sig in cookies / signed_request

I could not find the documentation on how to use this "utility". Can you tell me some documentation, or even better, give me an example of how this is used with Ruby / Sinatra?

+4
source share
2 answers

Almost all Graph API libraries that are available deal with signed_request in a similar way. Rest-Graph has a parse_signed_request method ( Rest-Graph / lib / core.rb ) that you can call in Sinatra.

I use Koala for this with Sinatra and it works as advertised:

 oauth = Koala::Facebook::OAuth.new(APP_ID, APP_CODE) signed_request = oauth.parse_signed_request(params["signed_request"]) 

You return the hash of the JSON object that Facebook publishes:

 { "algorithm"=>"HMAC-SHA256", "issued_at"=>1303883452, "user"=> { "country"=>"us", "locale"=>"en_US" }, "user_id"=>"100002364226618" } 

rest-graph also makes it pretty simple. Just tested it in the Sinatra app. Works great:

 rg = RestGraph.new( :app_id => APP_ID, :secret => APP_SECRET) parsed_request = rg.parse_signed_request!(params["signed_request"]) 

Lemme know this does not work for you.

+7
source

I just got an answer to this question from "cardinalblue" , a Rest-Graph gem developer. This little example was exactly what I was looking for:

 require 'sinatra' require 'rest-graph' app_id = '123' secret = 'abc' config = {:app_id => app_id, :secret => secret} post '/' do rg = RestGraph.new(config) rg.parse_signed_request!(params['signed_request']) "#{rg.get('me').inspect.gsub('<', '&lt;')}\n" end run Sinatra::Application 

Sidenote: if you are creating something similar, pay attention to post '/' do . Facebook pages retrieve your page using a POST request instead of a GET.

0
source

Source: https://habr.com/ru/post/1348081/


All Articles