Skip to content

Instantly share code, notes, and snippets.

@maraigue
Created February 17, 2010 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maraigue/306853 to your computer and use it in GitHub Desktop.
Save maraigue/306853 to your computer and use it in GitHub Desktop.
Sample of OAuth(XAuth) (Posting a message to Twitter)
#!/usr/bin/env ruby
# OAuth(XAuth)でTwitterに発言を投稿するサンプル
# ※xauth-twitter.rbが必要です
# http://gist.github.com/304123
#
# Sample of OAuth(XAuth) (Posting a message to Twitter)
# xauth-twitter.rb is required
# http://gist.github.com/304123
#
# Example:
# $ ruby post2twitter-oauth.rb Hello World!
User = "MyUserName"
Pass = "MyPassword"
require "kconv"
require "pp"
require "xauth-twitter"
module Post2Twitter
module_function
# Consumer key/secret of your OAuth application
ConsumerKey = "ABCDEFGH"
ConsumerSecret = "IJKLMNOPQRSTUVWXYZ"
def main(message)
if message.empty?
puts "No message specified"
return
end
consumer, access_token = XAuth.retrieve_access_token(User, Pass, ConsumerKey, ConsumerSecret)
response = access_token.post('http://twitter.com/statuses/update.json', {:status => message})
# response = access_token.post('http://twitter.com/statuses/update.json', {:status => message.toutf8}) # if from Windows[lang=ja]
begin
pp JSON.load(response.body)
rescue Exception => e
puts "Failed: #{e}"
end
end
end
if $0 == __FILE__
Post2Twitter.main(ARGV.join(' '))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment