-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendTweet.rb
40 lines (35 loc) · 898 Bytes
/
sendTweet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'rubygems'
require 'oauth'
require 'json'
#use your keys
consumer_key = OAuth::Consumer.new(
"XYZ",
"-")
access_token = OAuth::Token.new(
"XYZ",
"ABC")
#POST request
baseurl = "https://api.twitter.com"
path = "/1.1/statuses/update.json"
address = URI("#{baseurl}#{path}")
request = Net::HTTP::Post.new address.request_uri
request.set_form_data(
"status" => "woo hoo tweeted through Ruby",
)
# HTTP
http = Net::HTTP.new address.host, address.port
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
# final request
request.oauth! http, consumer_key, access_token
http.start
response = http.request request
#check respnse code
tweet = nil
if response.code == '200' then
tweet = JSON.parse(response.body)
puts "Successfully sent #{tweet["text"]}"
else
puts "Could not send the Tweet! " +
"Code:#{response.code} Body:#{response.body}"
end