Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've been using the vanilla OAuth gem to implement a few simple Twitter API calls. I didn't need a heavyweight gem to do everything, and I was already using OAuth, so a 'roll-your-own' approach seemed reasonable. I know that I haven't mentioned HTTParty, so please don't ding me for that. This may be useful to others for the essence of easy Twitter OAuth if you're already using the OAuth gem.</p> <p>In case it is helpful, here is the pertinent code (sorry about mixing some constants and other variables / methods at the start - it was the easiest and most accurate way to extract this from my real code):</p> <pre><code>#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 =&gt; "#{OAUTH_SITE}", :request_token_path=&gt;app_request_token_path, :authorize_path=&gt;app_authorize_path, :access_token_path=&gt;app_access_token_path, :http_method=&gt;:post, :scheme=&gt; :header, :body_hash =&gt; '' }) 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=&gt; 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 </code></pre> <p>I hope this, largely in context of broader use of the OAuth gem, might be useful to others. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload