Created
February 17, 2010 17:58
-
-
Save maraigue/306853 to your computer and use it in GitHub Desktop.
Sample of OAuth(XAuth) (Posting a message to Twitter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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