मैं कुछ सरल ट्विटर एपीआई कॉल लागू करने के लिए वेनिला ओथ मणि का उपयोग कर रहा हूं। मुझे सबकुछ करने के लिए हेवीवेट मणि की आवश्यकता नहीं थी, और मैं पहले से ही ओथ का उपयोग कर रहा था, इसलिए 'रोल-अप-खुद' दृष्टिकोण उचित लग रहा था। मुझे पता है कि मैंने HTTParty का उल्लेख नहीं किया है, इसलिए कृपया मुझे इसके लिए डिंग न करें। यदि आप पहले से ही ओथ मणि का उपयोग कर रहे हैं तो यह आसान ट्विटर ओएथ के सार के लिए दूसरों के लिए उपयोगी हो सकता है।
: -
मामले में यह उपयोगी है, यहाँ उचित कोड (यह मेरा असली कोड से निकालने के लिए सबसे आसान और सबसे सही तरीका था शुरू में कुछ स्थिरांक और अन्य चर/तरीकों मिश्रण के बारे में खेद है)
#Set up the constants, etc required for Twitter OAuth
OAUTH_SITE = "https://api.twitter.com"
TOKEN_REQUEST_METHOD = :post
AUTHORIZATION_SCHEME = :header
def app_request_token_path
"/oauth/request_token"
end
def app_authorize_path
"/oauth/authorize"
end
def app_access_token_path
"/oauth/access_token"
end
def consumer_key
"your twitter API key"
end
def consumer_secret
"your twitter API secret"
end
# Define the OAuth consumer
def consumer meth=:post
@consumer ||= OAuth::Consumer.new(consumer_key,consumer_secret, {
:site => "#{OAUTH_SITE}",
:request_token_path=>app_request_token_path,
:authorize_path=>app_authorize_path,
:access_token_path=>app_access_token_path,
:http_method=>:post,
:scheme=> :header,
:body_hash => ''
})
end
# Essential parts of a generic OAuth request method
def make_request url, method=:get, headers={}, content=''
if method==:get
res = @access_token.get(url, headers)
elsif method==:post
res = @access_token.post(url, content, headers)
end
if res.code.to_s=='200'
jres = ActiveSupport::JSON.decode(res.body)
if jres.nil?
@last_status_text = @prev_error = "Unexpected error making an OAuth API call - response body is #{res.body}"
end
return jres
else
@last_status_text = @prev_error = res if res.code.to_s!='200'
return nil
end
end
# Demonstrate the daily trends API call
# Note the use of memcache to ensure we don't break the rate-limiter
def daily_trends
url = "http://api.twitter.com/1/trends/daily.json"
@last_status_code = -1
@last_status_success = false
res = Rails.cache.fetch(url, :expires_in=> 5.minutes) do
res = make_request(url, :get)
unless res
@last_status_code = @prev_error.code.to_i
end
res
end
if res
@last_status_code = 200
@last_status_success = true
@last_status_text = ""
end
return res
end
मुझे उम्मीद है कि, ओएथ मणि के व्यापक उपयोग के संदर्भ में, यह दूसरों के लिए उपयोगी हो सकता है।
लगता है आप ट्विटर मणि को फोर्क कर सकते हैं और आपको जिस विधि की आवश्यकता है उसे जोड़ें (देखें: http://github.com/jnunemaker/twitter/blob/master/lib/twitter/base.rb)। किसी भी तरह से, यह देखने के लिए ट्विटर मणि एक अच्छी जगह हो सकती है कि यह कैसे किया जाता है (क्योंकि यह HTTParty और OAuth दोनों का उपयोग करता है) – Brian
असल में, ऐसा लगता है कि ट्विटर मणि की कार्यक्षमता आपके पास है, आप लाइनों को देखें 33-35 http://github.com/jnunemaker/twitter/blob/master/lib/twitter/base.rb – Brian
धन्यवाद। इससे पहले नहीं देखा>< –