【问题标题】:How To Subscribe To Real Time Updates For My App Users Feeds Using Koala Gem Rails如何使用 Koala Gem Rails 为我的应用用户订阅订阅实时更新
【发布时间】: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&gt;\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&gt;\n\u003Ch...", x-fb-trace-id: GbtUB+FcLJS [HTTP 400]

我不确定问题出在哪里,因为我看不到返回给 Facebook 的内容,而且我仍然有点不确定应该使用什么作为 VERIFY_TOKEN。

任何帮助表示赞赏。

【问题讨论】:

  • 您发送的响应显然是一个 HTML 文档,因为它以 \u003C!DOCTYPE html&gt;… 开头 - 但 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


【解决方案1】:

经过大量测试和卷曲后我发现了问题。

首先,问题是设计尝试进行身份验证并使用 HTML 错误文档进行响应。

然后我在这里使用了一段代码来响应请求,这很好但很旧。

以防万一有人错过,render_text: 现在是 render_plainWhat to use instead of `render :text` (and `render nothing: true`) in rails 5.1 and later?

我现在订阅了来自我的应用用户的更新,如果您想这样做,您可以使用上面的代码,只需记住跳过 FacebooksController 中的身份验证并使用 render_plain 而不是 render_text

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2014-09-03
    • 2013-02-15
    • 2016-12-16
    • 2012-08-08
    • 2021-07-15
    相关资源
    最近更新 更多