【发布时间】:2019-11-21 09:47:01
【问题描述】:
使用 Koala gem 从 Facebook 获取实时更新时遇到问题...
https://github.com/arsduo/koala/wiki/Realtime-Updates
我已按照 wiki 中的指南进行操作。我在路由文件中设置了一个回调 url:
match "facebook/subscription", :controller => :facebooks, :action => :subscribe, :as => 'subscribe', :via => [:get,:post]
然后,当 Facebook 发出获取请求时,我已经在我的控制器中尝试了以下两种方法,以按照他们的文档 (https://developers.facebook.com/docs/graph-api/webhooks/getting-started#verification-requests) 中的要求“迎接挑战”:
class FacebooksController < ApplicationController
def subscribe
verify_token = "VERIFY_TOKEN"
if params["hub.mode"] == "subscribe" && params["hub.verify_token"] == verify_token
params["hub.challenge"]
else
false
end
end
end
我也尝试过(将路线修改为正确的操作):
class FacebooksController < ApplicationController
def realtime_request?(request)
((request.method == "GET" && params['hub.mode'].present?) ||
(request.method == "POST" && request.headers['X-Hub-Signature'].present?))
end
def subscription
if(realtime_request?(request))
case request.method
when "GET"
challenge = Koala::Facebook::RealtimeUpdates.meet_challenge(params,'SOME_TOKEN_HERE')
if(challenge)
render :text => challenge
else
render :text => 'Failed to authorize facebook challenge request'
end
when "POST"
p params['object']
render :text => 'Thanks for the update.'
end
end
end
end
每次尝试订阅时,他们的仪表板上都会出现以下错误:
The URL couldn't be validated. Response does not match challenge, expected value="2090763306", received="\u003C!DOCTYPE html>\n\u003Chtm..."
当我尝试在控制台中运行它时,以下内容:
控制台命令:
@updates.subscribe("user", "feed", "https://www.bettrplay.com/facebook/subscription" , "YOUR_VERIFY_TOKEN")
控制台响应:
Koala::Facebook::ClientError: type: OAuthException, code: 2201, message: (#2201) response does not match challenge, expected value="773833772", received="\u003C!DOCTYPE html>\n\u003Ch...", x-fb-trace-id: GbtUB+FcLJS [HTTP 400]
我不确定问题出在哪里,因为我看不到返回给 Facebook 的内容,而且我仍然有点不确定应该使用什么作为 VERIFY_TOKEN。
任何帮助表示赞赏。
【问题讨论】:
-
您发送的响应显然是一个 HTML 文档,因为它以
\u003C!DOCTYPE html>…开头 - 但 API 希望您以纯文本形式的 just 挑战值响应,没有别的了。您需要弄清楚如何将其配置为不使用默认(?)页面模板进行此特定响应。 -
谢谢 misorude ...我设法找到了问题 - 动作控制器试图对用户进行身份验证以进行设计,因此返回错误 HTML 模板!现在我遇到了 500 错误的问题:``` ActionView::MissingTemplate (Missing template facebooks/subscribe, application/subscribe with {:locale=>[:en], :formats=>[:html], :variants= >[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder, :haml]}。```看起来 Rails 正在寻找要发送的模板?我试过 @ 987654330@ 但没有运气......有什么想法吗?
标签: ruby-on-rails ruby facebook facebook-graph-api koala