使用 Ruby/Sinatra 和 Rest-Graph gem 在 Facebook 应用程序中获取signed_request

发布于 2024-11-01 02:38:59 字数 595 浏览 1 评论 0原文

我使用 Sinatra 和 Rest-Graph gem 构建了一个 Facebook 应用程序。现在我想将应用程序作为 iframe 选项卡嵌入 Facebook 页面

为此,我需要从 Facebook 发送到我的应用程序的 signed_request 中获取数据。

Rest-Graph gem 在其 Github 页面上声明了以下功能:

提取access_token和的实用程序 检查 cookies/signed_request 中的签名

我找不到任何有关如何使用此“实用程序”的文档。你能给我指点一些文档吗?或者更好的是,给我一个关于如何在 Ruby/Sinatra 中使用它的示例?

I've built a Facebook app using Sinatra and the Rest-Graph gem. Now I would like to embed the app as an iframe tab in a Facebook Page.

To do that, I need to fetch data from the signed_request sent to my app by Facebook.

The Rest-Graph gem states the following feature on its Github page:

Utility to extract access_token and
check sig in cookies/signed_request

I couldn't find any documentation on how to use this "utility". Can you point me to some documentation or even better, give me an example on how this is used with Ruby/Sinatra?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木槿暧夏七纪年 2024-11-08 02:38:59

几乎所有可用的 Graph API 库都以类似的方式处理signed_request。 Rest-Graph 有一个 parse_signed_request 方法(Rest -Graph/lib/core.rb),您可以在 Sinatra 中调用它。

我使用 Koala 和 Sinatra 来完成此操作,它的工作原理与广告中所宣传的一样:

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

您可以返回 Facebook 发布的 JSON 对象的哈希值:

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

rest-graph 也使其变得非常简单。刚刚在 Sinatra 应用程序中对此进行了测试。完美运作:

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

让我知道这是否不适合您。

Nearly all of the 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'm using 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 get back a hash of the JSON object that Facebook posts:

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

rest-graph makes it pretty easy, too. Just tested this in a Sinatra app. Works perfectly:

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

Lemme know if that doesn't work for you.

风情万种。 2024-11-08 02:38:59

我刚刚收到来自“cardinalblue”的对该问题的回复< /a>,Rest-Graph gem 的开发者。这个小例子正是我正在寻找的:

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('<', '<')}\n"
end

run Sinatra::Application

旁注:如果您正在构建类似的东西,请注意 post '/' do。 Facebook 页面使用 POST 请求而不是 GET 来获取您的页面。

I just got a response to this question from "cardinalblue", the developer of the Rest-Graph gem. 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('<', '<')}\n"
end

run Sinatra::Application

Sidenote: In case you're building something similar, please note the post '/' do. Facebook Pages fetch your page using a POST request instead of a GET.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文