File: oauth-tweet.rb

package info (click to toggle)
ruby-em-http-request 0.3.0-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 424 kB
  • sloc: ruby: 2,381; ansic: 999; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,533 bytes parent folder | download
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
41
42
43
44
45
46
47
48
49
50
# Courtesy of Darcy Laycock:
# http://gist.github.com/265261
#

require 'rubygems'

require 'em-http'
require 'oauth'

# At a minimum, require 'oauth/request_proxy/em_http_request'
# for this example, we'll use Net::HTTP like support.
require 'oauth/client/em_http'

# You need two things: an oauth consumer and an access token.
# You need to generate an access token, I suggest looking elsewhere how to do that or wait for a full tutorial.
# For a consumer key / consumer secret, signup for an app at:
# http://twitter.com/apps/new

# Edit in your details.
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""

def twitter_oauth_consumer
  @twitter_oauth_consumer ||= OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => "http://twitter.com")
end

def twitter_oauth_access_token
  @twitter_oauth_access_token ||= OAuth::AccessToken.new(twitter_oauth_consumer, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
end

EM.run do

  request = EventMachine::HttpRequest.new('http://twitter.com/statuses/update.json')
  http = request.post(:body => {'status' => 'Hello Twitter from em-http-request with OAuth'}, :head => {"Content-Type" => "application/x-www-form-urlencoded"}) do |client|
    twitter_oauth_consumer.sign!(client, twitter_oauth_access_token)
  end

  http.callback do
    puts "Response: #{http.response} (Code: #{http.response_header.status})"
    EM.stop_event_loop
  end

  http.errback do
    puts "Failed to post"
    EM.stop_event_loop
  end

end