我的導軌應用程序和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