在 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'
我想请你帮忙如何更新令牌。在这种情况下,将令牌存储在数据库中的最佳方法是在到期前一天运行此操作并更新令牌吗?或者有没有更好的方法来做到这一点?
太感谢了