Rails + Facebook:如何续订Facebook访问令牌?

时间:2013-09-08 12:24:32

标签: ruby-on-rails ruby facebook access-token facebook-access-token

在Rails应用程序中,我们实施了Facebook API。我们有一个Facebook页面,当用户将某些内容发布到Rails应用程序时,我们需要自动发布状态。问题是,我们需要自动续订Facebook访问令牌。 我试图这样做,但是当我第一次这样做时,我有点迷失在这里:

def fb_autopost    
    require 'net/http'
    temp_token = 'my_short_time_token'


    app_id = '145634995501123' # I didn't put here real token from obvious reason
    app_secret = '0dd5fcf93b4280fb19bf6b80f487c123' # I didn't put here real token from obvious reason
    puts "https://graph.facebook.com/oauth/access_token?client_id=#{app_id}&client_secret=#{app_secret}&grant_type=fb_exchange_token&fb_exchange_token=#{temp_token}"
    puts '---'
    url = "https://graph.facebook.com/oauth/access_token?client_id=#{app_id}&client_secret=#{app_secret}&grant_type=fb_exchange_token&fb_exchange_token=#{temp_token}"
    response = open(url)
    puts response.inspect
    puts '---'  
  end

当我运行此代码时,我收到此错误:

Processing by ListingsController#post_to_fb as HTML
Completed 500 Internal Server Error in 1015ms

OpenURI::HTTPError (400 Bad Request):
  app/controllers/listings_controller.rb:194:in `fb_autopost'

我想请教您如何续订令牌。在这种情况下,是将令牌存储在数据库中的最佳方式,例如在到期前一天运行此操作并续订令牌? 或者有更好的方法吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

我个人使用这个宝石:https://github.com/nov/fb_graph

您应该在文档中找到所需的一切(https://github.com/nov/fb_graph#normal-oauth2-flow

相关问题