Question

I'm having a really weird problem with my rails app and facebook's open graph beta. Whenever I post an action to an object, Facebook returns an error that seems to indicate the URL of the object can't be reached, or the og scraper isn't scraping the URL correctly.

However, when I take the URL the app is generating for the post to Facebook and manually use the HTTParty gem to post it, it works.

Here's my code:

class Post < ActiveRecord::Base
  FB_CONFIG = YAML.load_file("#{Rails.root}/config/initializers/facebook.yml")[Rails.env]

  def self.to_facebook_og(obj, obj_id, verb, auth, extra)
    #requires that a user has granted `publish_actions`
    found_obj = obj.classify.constantize.find(obj_id) #find the actual object we're talking about
    post_url = self.construct_facebook_action_url(obj, found_obj, verb, auth, extra) #create the URL
    begin
      ret = HTTParty.post(post_url)
      logger.info "Facebook Post Action Response = #{ret}"
    rescue HTTParty::ResponseError => e #handle any errors
      logger.error {"FACEBOOK Response #{ret.code} / #{e.inspect}"}
      flash.alert {"There was a Facebook problem. Please try again."}
      return
    end
  end

  def self.construct_facebook_action_url(obj, found_obj, verb, auth, extra)
    base = 'https://graph.facebook.com/'
    uid = auth.uid
    namespace = FB_CONFIG['namespace']
    token = "?access_token=#{auth.token}"
    og_url = "#{obj}=http://theshortestfiction.com/#{obj.pluralize}/#{found_obj.id}"
    fb_url = base + uid + '/' + namespace + ':' + verb + token + '&' + og_url + extra
    logger.info fb_url
    fb_url
  end

  def self.lint_og_object(obj_url)
   lint_ret = HTTParty.post("https://developers.facebook.com/tools/lint/?url=#{obj_url}&format=json")
   logger.info "Facebook Linter Response = #{lint_ret}"
  end



end

When an object is read via its controller's show method, the app calls Post.to_facebook. From my logs, I can see that that Post.construct_facebook_action_url is constructing the proper url (because like I said, I can pull the URL from the logs and manually post it from the console). So, I'm assuming there's so problem with how I'm passing the URL to HTTParty? Facebook seems able to tell what object URL it should be looking at. Why does the code I've written not work, but manually in the console, it does?

Even weirder -- once there's been a sucessful post action on the object once, the code seems to work consistently. Facebook insists the problem is that the objects' URLs aren't reachable, but I can't understand how they're not, since I can browse to them.

Was it helpful?

Solution

I think this is actually a timeout issue.

I had the exact same issue as you, using HTTParty and getting the URL can't be reached error.

I moved the code to a background process using Resque and it fixed the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top