2010-08-09 17 views
7

क्या HTTPAy के साथ OAuth का उपयोग करना संभव है? मैं this एपीआई कॉल करने की कोशिश कर रहा हूं, लेकिन, दस्तावेज़ीकरण के विपरीत, इसे प्रमाणीकरण की आवश्यकता है।ओएथ और HTTParty

इससे पहले कि आप "ट्विटर-विशिष्ट जेम का उपयोग करें" कहें, मुझे सुनें - मैंने कोशिश की है। मैंने ट्विटर, गले और अनगिनत अन्य लोगों की कोशिश की है, लेकिन कोई भी इस विशिष्ट एपीआई कॉल का समर्थन नहीं करता है। तो, मैं HTTParty में बदल गया है।

तो, मैं HTTPAart के साथ OAuth का उपयोग कैसे कर सकता हूं?

+0

लगता है आप ट्विटर मणि को फोर्क कर सकते हैं और आपको जिस विधि की आवश्यकता है उसे जोड़ें (देखें: http://github.com/jnunemaker/twitter/blob/master/lib/twitter/base.rb)। किसी भी तरह से, यह देखने के लिए ट्विटर मणि एक अच्छी जगह हो सकती है कि यह कैसे किया जाता है (क्योंकि यह HTTParty और OAuth दोनों का उपयोग करता है) – Brian

+1

असल में, ऐसा लगता है कि ट्विटर मणि की कार्यक्षमता आपके पास है, आप लाइनों को देखें 33-35 http://github.com/jnunemaker/twitter/blob/master/lib/twitter/base.rb – Brian

+0

धन्यवाद। इससे पहले नहीं देखा>< –

उत्तर

1

मुझे नहीं लगता कि HTTParty OAuth का समर्थन करता है (हालांकि मैं HTTParty पर कोई विशेषज्ञ नहीं हूं, यह बहुत उच्च स्तर और मेरे स्वाद के लिए धीमा है)।

मैं सिर्फ OAuth gem का उपयोग कर ट्विटर अनुरोध को सीधे कॉल करूंगा। ट्विटर एपीआई दस्तावेज में भी उपयोग का एक उदाहरण है: https://dev.twitter.com/docs/auth/oauth/single-user-with-examples#ruby

3

मैं कुछ सरल ट्विटर एपीआई कॉल लागू करने के लिए वेनिला ओथ मणि का उपयोग कर रहा हूं। मुझे सबकुछ करने के लिए हेवीवेट मणि की आवश्यकता नहीं थी, और मैं पहले से ही ओथ का उपयोग कर रहा था, इसलिए 'रोल-अप-खुद' दृष्टिकोण उचित लग रहा था। मुझे पता है कि मैंने 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 

मुझे उम्मीद है कि, ओएथ मणि के व्यापक उपयोग के संदर्भ में, यह दूसरों के लिए उपयोगी हो सकता है।

+0

इस्माइल अब्रू संपादित करने के लिए धन्यवाद। यह पकड़कर कि मैंने इसे अन्य कोड से कितना खराब निकाला। – Phil

+0

अरे, मुझे पता है कि यह एक वर्ष रहा है, लेकिन क्या आप मुझे बता सकते हैं कि आपने '@ access_token' निर्दिष्ट किया है? मैं आपका उदाहरण प्यार कर रहा हूं और एक समस्या को लक्षित करता हूं, मैं अभी अपने सिर को मार रहा हूं। – BenMorganIO

+0

मेरी इच्छा है कि मैं आपकी मदद कर सकता हूं। मेरा कोड मूल से बहुत अलग हो गया है क्योंकि मैंने जवाब दिया था कि क्या यह चल रहा था कि दिव्य के लिए यह कठिन है। लेकिन जब आप एक्सेस टोकन के लिए अनुरोध टोकन का आदान-प्रदान करते हैं, तो यह आपके ऐप और ट्विटर के बीच एक्सचेंज में तीसरा कदम था। शायद ये स्निपेट मदद करते हैं: 'request_token = params [: oauth_token]; oauth_options = {: oauth_verifier => पैराम्स [: oauth_verifier]}; OAuth :: RequestToken.new (उपभोक्ता, request_token, उपभोक्ता_सेक्रेट); @access_token = request_token.get_access_token ({: http_method => TOKEN_REQUEST_METHOD}, पैराम्स) ' – Phil

0

मैं प्रमाणीकरण टोकन प्राप्त करने OAuth2 मणि ​​का मिश्रण का इस्तेमाल किया और HTTParty एक लंबा रास्ता द्वारा क्वेरी

client = OAuth2::Client.new(apiKey, apiSecret, 
          :site => "https://SiteForAuthentication.com") 
oauthResponse = client.password.get_token(username, password) 
token = oauthResponse.token 

queryAnswer = HTTParty.get('https://api.website.com/query/location', 
          :query => {"token" => token}) 

सही नहीं बनाने के लिए, लेकिन यह अब तक चाल किया है

संबंधित मुद्दे