首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OAuth和HTTParty

OAuth和HTTParty
EN

Stack Overflow用户
提问于 2010-08-10 00:13:30
回答 3查看 5.7K关注 0票数 10

可以在HTTParty中使用OAuth吗?我正在尝试执行this应用程序接口调用,但与文档相反,它需要身份验证。

在你说“使用特定于Twitter的Gem”之前,先听我说完--我已经试过了。我尝试过twitter、grackle和无数其他工具,但都不支持这个特定的API调用。所以,我求助于HTTParty。

那么,如何在HTTParty中使用OAuth呢?

EN

回答 3

Stack Overflow用户

发布于 2012-07-28 05:43:35

我一直在使用普通的OAuth gem来实现一些简单的Twitter API调用。我并不需要一个重量级的gem来做所有的事情,而且我已经在使用OAuth了,所以“自己滚”的方法似乎是合理的。我知道我没有提到HTTParty,所以请不要因为这个而责备我。如果您已经在使用OAuth gem,这可能对其他人学习easy Twitter的精髓很有用。

如果它是有用的,这里是相关的代码(抱歉在开始时混合了一些常量和其他变量/方法-这是从我的真实代码中提取它的最简单和最准确的方法):

代码语言:javascript
复制
#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的更广泛使用的背景下进行的,可能会对其他人有用。

票数 3
EN

Stack Overflow用户

发布于 2012-06-18 17:50:38

我不认为HTTParty支持OAuth (虽然我不是HTTParty方面的专家,但对我来说它太高级太慢了)。

我只需要使用OAuth gem直接调用Twitter请求。Twitter API文档甚至有一个用法示例:https://dev.twitter.com/docs/auth/oauth/single-user-with-examples#ruby

票数 1
EN

Stack Overflow用户

发布于 2015-10-06 00:23:09

我混合使用了OAuth2 gem来获取身份验证令牌和HTTParty来进行查询

代码语言:javascript
复制
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})

虽然还不是很完美,但到目前为止似乎已经成功了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3442002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档