Skip to content

Instantly share code, notes, and snippets.

@srikanthps
Created June 18, 2011 17:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srikanthps/1033288 to your computer and use it in GitHub Desktop.
Save srikanthps/1033288 to your computer and use it in GitHub Desktop.
Ruby Application to create twitter tweets - "Tweeter"
Tweeter Application:
This application can be used to create tweet programmatically.
Pre-requisites:
==============
1. In order to use it, you need to have "twitter_oauth" gem installed.
gem install twitter_auth
2. Register an application on http://dev.twitter.com/. Select the "Application Type" as "client", as this program does is not a web-based application and hence, cannot have call back URL as needed for browser apps.
3. After you register an application, note down the values for "Consumer key" and "Consumer secret"
Usage:
=====
1. Download the "tweeter.rb" and "tweeter_example.rb".
2. Download the "twitter_app_info.yaml", and fill in the values of consumer key and secret.
3. Run the "twitter_example.rb"
ruby twitter_example.rb
Trouble-shooting:
================
1. Line 26 of "tweeter.rb" launches a browser so twitter user (you if you are running the sample program) can go and authorize the app. This code works only on Mac OS. Comment it if you have issues, and launch the browser and visit the URL printed in the console.
require 'rubygems'
require 'yaml'
require 'net/http'
require 'twitter_oauth'
APP_INFO_FILE = "twitter_app_info.yaml"
def get_user_authorization
app_info = get_app_info
# Create an OAuth client using your twitter app details
client = TwitterOAuth::Client.new(
:consumer_key => app_info[:consumer_key],
:consumer_secret => app_info[:consumer_secret]
)
# Request a token for Out-of-band access (Desktop app mode, not browser mode)
request_token = client.request_token(:oauth_rollback => "oob")
puts "Visit #{request_token.authorize_url} and get hold of authorization PIN"
puts " for Token: #{request_token.token}"
#puts "Secret: #{request_token.secret}"
# Launch a browser and let the user get a PIN from twitter.com
system "open", request_token.authorize_url
# User can now enter PIN here so that app can be authorized
puts "Get the PIN number from the browser just launched, and enter here:"
pin = gets
# Authorize the client
access_token = client.authorize(
request_token.token,
request_token.secret,
:oauth_verifier => pin.strip
)
# Check for success of authorization
user_access_info = {}
if client.authorized? then
puts "User #{access_token.params[:screen_name]} has authorized this app"
puts "Store Token #{access_token.token} and Secret #{access_token.secret} for the user."
user_access_info = {
:screen_name => access_token.params[:screen_name],
:token => access_token.token,
:secret => access_token.secret
}
user_file = "#{user_access_info[:screen_name]}.yaml"
puts "Writing file #{user_file}"
File.open(user_file, "w+") do |out|
YAML.dump(user_access_info, out)
end
end
user_access_info
end
def get_app_info
app_info = {}
begin
app_info = YAML.load(File.open(APP_INFO_FILE, "r"))
rescue
puts "ERROR: #{APP_INFO_FILE} not found."
exit
end
app_info
end
def read_user_access_info(user)
info = nil
begin
info = YAML.load(File.open("#{user}.yaml", "r"))
rescue
ensure
puts "#{user}.yaml not found" if info.nil?
p info
end
info
end
def tweet(user, message)
user_info = read_user_access_info(user) || get_user_authorization
app_info = get_app_info
client = TwitterOAuth::Client.new(
:consumer_key => app_info[:consumer_key],
:consumer_secret => app_info[:consumer_secret],
:token => user_info[:token],
:secret => user_info[:secret]
)
if (client.authorized?)
puts "Tweeting #{message}..."
client.update(message)
puts "Done, check the twitter page!"
else
puts "CLient not authorized. Check tokens stored in user file."
puts "If required, delete the user file and re-run this programs"
end
end
require 'rubygems'
require 'net/http'
require 'twitter_oauth'
require 'tweeter'
puts "Enter Twitter ID#: "
twitter_id = gets
twitter_id.strip!
puts "User id is #{twitter_id}"
tweet(twitter_id, "Hello world!")
:consumer_key : 'YOUR_TWITTER_APPLICATION_CONSUMER_KEY_HERE'
:consumer_secret : 'YOUR_TWITTER_APPLICATION_CONSUMER_SECRET_HERE'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment