0

Having trouble getting the real time updates from Facebook working using Koala gem...

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

I have followed the guide in the wiki. I have set up a callback url with the following in my routes file:

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

Then when Facebook makes the get request I have tried both the following in my controller to 'meet the challenge' as required in their docs (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

I have also tried (with the route amended to the right action):

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 result every time is the following error on their dashboard when I try to subscribe:

The URL couldn't be validated. Response does not match challenge, expected value="2090763306", received="\u003C!DOCTYPE html>\n\u003Chtm..."

And the following when I try to run this in the console:

Console cmd: @updates.subscribe("user", "feed", "https://www.bettrplay.com/facebook/subscription" , "YOUR_VERIFY_TOKEN")

Console response: 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]

I am unsure what the issue is as I can not see what is being returned to Facebook and i am still a little unsure of what I should be using as the VERIFY_TOKEN.

Any help appreciated.

Gareth32
  • 51
  • 7
  • The response you are sending is obviously an HTML document, since it starts with `\u003C!DOCTYPE html>…` - but the API expects you to respond with _just_ the challenge value as plain text, and nothing else. You need to figure out how to configure it not to use your default(?) page template for this particular response. – misorude Jul 12 '19 at 06:56
  • Thanks misorude ... I managed to find the problem - the Action Controller was trying to authenticate the user for devise so returning the error HTML template for that! Now I have a problem with a 500 error: ``` ActionView::MissingTemplate (Missing template facebooks/subscribe, application/subscribe with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder, :haml]}. ``` Looks like Rails is looking for a template to send? I have tried `render text: challenge, , content_type: 'text/plain'` but no luck... Any ideas? – Gareth32 Jul 12 '19 at 13:50

1 Answers1

0

OK after much testing and cURLing I found the problem.

First the problem was devise trying to authenticate and so responding with the HTML error doc.

Then I had used a piece of code off here to respond to the request, which was fine but old.

Just in case anyone missed it, render_text: is now render_plain : What to use instead of `render :text` (and `render nothing: true`) in rails 5.1 and later?

I am now subscribed for updates from my app users and if you want to do the same you can use the code above, just remember to skip authentication in the FacebooksController and use render_plain instead of render_text!

Gareth32
  • 51
  • 7