module TwitterOAuthToken

Get Twitter OAuth token

Command line interface

Get Twitter OAuth token

Project constants

Constants

PROJECT
PROMPT_CONSUMER_KEY

prompt keys

PROMPT_CONSUMER_KEY_DEFAULT

prompt defaults

PROMPT_CONSUMER_SECRET
PROMPT_CONSUMER_SECRET_DEFAULT
PROMPT_OPEN_URL
PROMPT_OPEN_URL_DEFAULT
PROMPT_PIN
PROMPT_PIN_DEFAULT
RESULT_ACCESS_TOKEN
RESULT_ACCESS_TOKEN_SECRET
RESULT_ERROR
VERSION

Public Class Methods

access_token(request_token, pin) click to toggle source
# File lib/twitter_oauth_token/get.rb, line 38
def access_token(request_token, pin)
  request_token.get_access_token(
    :oauth_token => request_token.token,
    :oauth_verifier => pin
  )
end
authorize_url(request_token) click to toggle source
# File lib/twitter_oauth_token/get.rb, line 34
def authorize_url(request_token)
  request_token.authorize_url
end
cli() click to toggle source
# File lib/twitter_oauth_token/cli.rb, line 7
def cli
  puts "#{PROJECT} #{VERSION}"
  puts 'Let\'s get that token in 4 easy steps!'
  results = get()

  error = results[RESULT_ERROR]

  if error.nil?
    access_token = results[RESULT_ACCESS_TOKEN]
    access_token_secret = results[RESULT_ACCESS_TOKEN_SECRET]

    puts "Token: #{access_token}"
    puts "Secret: #{access_token_secret}"
    puts '🎉'
  else
    puts "Error: #{error}"
  end
end
consumer(consumer_key, consumer_secret) click to toggle source
# File lib/twitter_oauth_token/get.rb, line 22
def consumer(consumer_key, consumer_secret)
  OAuth::Consumer.new(
    consumer_key,
    consumer_secret,
    :site => 'https://api.twitter.com'
  )
end
get(prompts=nil) click to toggle source
# File lib/twitter_oauth_token/get.rb, line 45
def get(prompts=nil)
  if prompts.nil?
    prompts = {
      PROMPT_CONSUMER_KEY => PROMPT_CONSUMER_KEY_DEFAULT,
      PROMPT_CONSUMER_SECRET => PROMPT_CONSUMER_SECRET_DEFAULT,
      PROMPT_OPEN_URL => PROMPT_OPEN_URL_DEFAULT,
      PROMPT_PIN => PROMPT_PIN_DEFAULT
    }
  end

  begin
    print prompts[PROMPT_CONSUMER_KEY]
    consumer_key = STDIN.gets.strip

    print prompts[PROMPT_CONSUMER_SECRET]
    consumer_secret = STDIN.gets.strip

    c = consumer(consumer_key, consumer_secret)

    request_token = request_token(c)

    url = authorize_url(request_token)
    puts "#{prompts[PROMPT_OPEN_URL]}#{url}"

    print prompts[PROMPT_PIN]
    pin = STDIN.gets.strip

    access_token = access_token(request_token, pin)

    {
      RESULT_ACCESS_TOKEN => access_token.token,
      RESULT_ACCESS_TOKEN_SECRET => access_token.secret,
      RESULT_ERROR => nil
    }
  rescue => e
    {
      RESULT_ACCESS_TOKEN => nil,
      RESULT_ACCESS_TOKEN_SECRET => nil,
      RESULT_ERROR => e
    }
  end # begin
end
request_token(consumer) click to toggle source
# File lib/twitter_oauth_token/get.rb, line 30
def request_token(consumer)
  consumer.get_request_token
end