使用HTTParty发布到Facebook OG只能在rails控制台上运行?

时间:2012-01-13 17:29:45

标签: ruby-on-rails-3 facebook-graph-api httparty

我的rails应用程序和facebook的开放式测试版有一个非常奇怪的问题。每当我向某个对象发布操作时,Facebook都会返回一个错误,该错误似乎表明无法访问该对象的网址,或者og scraper无法正确抓取该网址。

然而,当我拿到应用程序为Facebook发布帖子并手动使用HTTParty gem发布它的URL时,它可以正常工作。

这是我的代码:

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

当通过其控制器的show方法读取对象时,应用程序会调用Post.to_facebook。从我的日志中,我可以看到Post.construct_facebook_action_url正在构建正确的URL(因为就像我说的,我可以从日志中提取URL并从控制台手动发布)。所以,我假设我如何将网址传递给HTTParty存在问题? Facebook似乎能够告诉应该正在查看哪个对象网址。为什么我写的代码不起作用,但在控制台中手动操作,它呢?

甚至更奇怪 - 一旦在对象上进行过一次成功的后期操作,代码似乎始终如一。 Facebook 坚持问题在于对象&#39;网址无法访问,但由于我可以浏览到这些网址,因此我无法理解这些网址。

1 个答案:

答案 0 :(得分:0)

我认为这实际上是一个超时问题。

我遇到与您完全相同的问题,使用HTTParty并且无法获取URL错误。

我使用Resque将代码移动到后台进程,它解决了问题。