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.
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 nowrender_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_plaininstead ofrender_text!