Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'twitter_oauth'
- require 'yaml'
- require 'fileutils'
- $CONFIG_FILE = 'config.yaml'
- def load_config(config_file)
- # if the config file doesn't exist, we'll need to get our access token
- unless File.exists?(config_file)
- create_default_config_file($CONFIG_FILE)
- get_access_token
- end
- begin
- $CONFIG = YAML::load(File.read(config_file))
- $consumer_key = $CONFIG['oauth']['consumer_key']
- $consumer_secret = $CONFIG['oauth']['consumer_secret']
- $request_token = $CONFIG['oauth']['request_token']
- $request_secret = $CONFIG['oauth']['request_secret']
- rescue
- create_default_config_file($CONFIG_FILE)
- end
- if $request_token.nil? and $request_secret.nil?
- get_access_token
- # not yet authorized
- else
- # authorized
- end
- end
- def create_default_config_file(config_file)
- File.open(config_file, 'w') do |out|
- $CONFIG['oauth']['consumer_key'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'
- $CONFIG['oauth']['consumer_key'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
- YAML::dump($CONFIG, out)
- end
- load_config($CONFIG_FILE)
- end
- def get_access_token()
- unless File.exists?($CONFIG_FILE)
- create_default_config_file($CONFIG_FILE)
- end
- client = TwitterOAuth::Client.new(:consumer_key => $consumer_key, :consumer_secret => $consumer_secret)
- request_token = client.request_token
- puts "Please open the following address in your browser to authorize this application:"
- puts "#{request_token.authorize_url}\n"
- puts "Enter the PIN from twitter.com and press [ENTER]"
- pin = STDIN.gets.strip
- $access_token = client.authorize(request_token.token, request_token.secret)
- File.open($CONFIG_FILE, 'w') do |out|
- $CONFIG['oauth']['request_token'] = access_token.token
- $CONFIG['oauth']['request_secret'] = access_token.secret
- YAML::dump($CONFIG, out)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement