10

HTTPartyでOAuthを使用することは可能ですか?このAPI呼び出しを実行しようとしていますが、ドキュメントと矛盾するため、認証が必要です。

「Twitter固有の宝石を使用する」と言う前に、私に聞いてください-私は試しました。twitter、grackle、その他数え切れ​​ないほどのことを試しましたが、この特定のAPI呼び出しをサポートしているものはありません。そこで、HTTPartyに目を向けました。

では、HTTPartyでOAuthを使用するにはどうすればよいですか?

4

3 に答える 3

3

私はいくつかの単純な Twitter API 呼び出しを実装するためにバニラ OAuth gem を使用してきました。すべてを行うのに重量級の gem は必要ありませんでした。また、すでに 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 の幅広い使用のコンテキストで、他の人に役立つことを願っています。

于 2012-07-27T21:43:34.557 に答える
1

私は、HTTParty が OAuth をサポートしているとは思いません (私は HTTParty の専門家ではありませんが、私の好みではレベルが高すぎて遅すぎます)。

OAuth gemを使用して Twitter リクエストを直接呼び出すだけです。Twitter API ドキュメントには使用例もあります: https://dev.twitter.com/docs/auth/oauth/single-user-with-examples#ruby

于 2012-06-18T09:50:37.583 に答える
0

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})

長い道のりで完璧ではありませんが、これまでのところトリックを行っているようです

于 2015-10-05T16:23:09.347 に答える