OAuth和HTTParty

是否可以在HTTParty中使用OAuth?我正在尝试执行此API调用,但与文档相矛盾,它需要身份验证。 在你说“使用特定于Twitter的宝石”之前,请听我说 - 我试过了。我尝试了twitter,grackle和无数其他人,但没有人支持这个特定的API调用。所以,我转向了HTTParty。 那么,我如何在HTTParty中使用OAuth?     
已邀请:
我一直在使用vanilla OAuth gem来实现一些简单的Twitter API调用。我不需要一个重量级的宝石来做任何事情,而且我已经在使用OAuth,所以“自己动手”的方法似乎是合理的。我知道我没有提到过HTTParty,所​​以请不要因此而告诉我。如果您已经在使用OAuth gem,那么对于其他人来说,这对于简单的Twitter OAuth的本质可能是有用的。 如果它有用,这里是相关的代码(抱歉在开始时混合一些常量和其他变量/方法 - 这是从我的真实代码中提取这个的最简单和最准确的方法):
#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  
我希望这主要是在更广泛地使用OAuth gem的背景下,可能对其他人有用。     
我认为HTTParty并不支持OAuth(虽然我不是HTTParty的专家,但它太高级了,而且我的口味很慢)。 我只是直接使用OAuth gem来调用Twitter请求。 Twitter API文档甚至还有一个使用示例:https://dev.twitter.com/docs/auth/oauth/single-user-with-examples#ruby     
我使用了OAuth2 gem的混合来获取身份验证令牌,使用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})
从长远来看并不完美,但到目前为止似乎已经完成了这个伎俩     

要回复问题请先登录注册