是否可以在HTTParty中使用OAuth?我正在嘗試執行this API調用,但與文檔相矛盾,它需要身份驗證。OAuth和HTTParty
在你說「使用Twitter特有的寶石」之前,請聽我說 - 我已經嘗試過了。我試過twitter,grackle和其他無數人,但沒有人支持這個特定的API調用。所以,我轉向了HTTParty。
那麼,我怎麼能用HTTParty使用OAuth?
是否可以在HTTParty中使用OAuth?我正在嘗試執行this API調用,但與文檔相矛盾,它需要身份驗證。OAuth和HTTParty
在你說「使用Twitter特有的寶石」之前,請聽我說 - 我已經嘗試過了。我試過twitter,grackle和其他無數人,但沒有人支持這個特定的API調用。所以,我轉向了HTTParty。
那麼,我怎麼能用HTTParty使用OAuth?
我不認爲HTTParty支持OAuth(雖然我不是HTTParty的專家,但它對我的口味來說過於高級和緩慢)。
我只是直接使用OAuth gem來致電Twitter請求。 Twitter API文檔甚至有一個使用示例:https://dev.twitter.com/docs/auth/oauth/single-user-with-examples#ruby
我一直在使用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寶石的情況下,可能對其他人有用。
感謝您編輯Ismael Abreu。捕捉到我從其他代碼中提取的這個數據很糟糕。 – Phil 2013-07-18 18:24:35
嘿,我知道它已經過了一年了,但是你能告訴我你指定'@ access_token'的位置嗎?我非常喜歡你的榜樣,並且針對我現在正在打我頭的問題。 – BenMorganIO 2014-05-21 06:38:06
我希望我可以幫助你。我的代碼與原來的代碼差別很大,因爲我回答說很難理解正在發生的事情。但是,當您爲訪問令牌交換請求令牌時,這是您的應用和Twitter之間交換的第三步。也許這些片段有幫助:'request_token = params [:oauth_token]; oauth_options = {:oauth_verifier => params [:oauth_verifier]}; OAuth :: RequestToken.new(consumer,request_token,consumer_secret); @access_token = request_token.get_access_token({:http_method => TOKEN_REQUEST_METHOD},params)' – Phil 2014-05-28 15:43:56
我用的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})
並不完美,但它似乎做的伎倆到目前爲止
你可以爲twitter創建分支並添加所需的方法(請參閱:http://github.com/jnunemaker/twitter/blob/master/lib/twitter/base.rb)。無論哪種方式,Twitter的寶石可能是一個很好的地方,看看如何做到這一點(因爲它使用HTTParty和OAuth) – Brian 2010-08-09 18:04:28
實際上,它看起來像Twitter的寶石確實有你正在尋找的功能,見行33-35 http://github.com/jnunemaker/twitter/blob/master/lib/twitter/base.rb – Brian 2010-08-09 18:15:35
謝謝。沒有注意到之前>。< – 2010-08-11 12:55:24