如何使用Koala Gem Rails为我的App用户订阅实时更新源

时间:2019-07-11 15:58:49

标签: ruby-on-rails ruby facebook facebook-graph-api koala

使用Koala gem从Facebook获取实时更新时遇到麻烦...

https://github.com/arsduo/koala/wiki/Realtime-Updates

我已遵循Wiki中的指南。我在路由文件中设置了以下内容的回调网址:

match "facebook/subscription", :controller => :facebooks, :action => :subscribe, :as => 'subscribe', :via => [:get,:post]

然后,当Facebook发出get请求时,我已经尝试在控制器中执行以下两项操作,以按照其文档(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..."

然后尝试在控制台中运行以下命令:

控制台cmd: @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。

任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

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

首先,问题是设计出试图进行身份验证并因此响应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

相关问题