0

我的 Rails 应用程序和 facebook 的开放图形测试版遇到了一个非常奇怪的问题。每当我向对象发布操作时,Facebook 都会返回一个错误,似乎表明无法访问该对象的 URL,或者 og 抓取工具没有正确抓取 URL。

但是,当我将应用程序为帖子生成到 Facebook 的 URL 并手动使用 HTTParty gem 发布它时,它可以工作。

这是我的代码:

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 并从控制台手动发布它)。所以,我假设我如何将 URL 传递给 HTTParty 有这么大的问题?Facebook 似乎能够告诉它应该查看哪个对象 URL。为什么我写的代码不起作用,但在控制台手动,它呢?

更奇怪的是——一旦对对象进行了一次成功的 post 操作,代码似乎可以始终如一地工作。Facebook坚持认为问题在于对象的 URL 不可访问,但我不明白它们是如何不可访问的,因为我可以浏览到它们。

4

1 回答 1

0

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

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

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

于 2012-03-29T18:29:25.300 回答