我的导轨应用程序和Facebook的开放图测试版有一个非常奇怪的问题。每当我发布一个动作到一个对象,Facebook返回一个错误,似乎表明该对象的URL无法到达,或og scraper没有正确地抓取URL。使用HTTParty发布到Facebook OG只能在rails控制台上使用?
但是,当我将该应用程序生成的URL发送到Facebook并手动使用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
当通过其控制的显示方法读取对象,应用程序调用Post.to_facebook
。从我的日志中,我可以看到Post.construct_facebook_action_url
正在构建正确的url(因为就像我所说的,我可以从日志中提取URL并手动将其从控制台发布)。那么,我假设我将URL传递给HTTParty存在这样的问题? Facebook似乎能够告诉它应该在什么对象URL 正在看。为什么我写的代码无法正常工作,但是在控制台中手动执行?
即使是怪物 - 一旦对对象进行了一次成功的后处理,代码似乎一直运行。 Facebook 坚持问题在于对象的URL无法访问,但我无法理解它们不是如何,因为我可以浏览它们。
我真的把它移到了预定的过程中,它似乎正在工作。 – Slick23 2012-03-29 23:07:12