Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Requesting OAuth access token without browser, only with Twitter user name and password (so-called xAuth), by Ruby
#!/usr/bin/env ruby
# *** Important notification ***
# As xAuth is officially supported, we have to register
# your application needing xAuth, via e-mail.
# If not registered, 401 error will be returned.
# http://apiwiki.twitter.com/Twitter-REST-API-Method:-oauth-access_token-for-xAuth
#
# (*** 重要な告知 ***
# xAuthが公式にサポートされたのに伴い、xAuthの必要なアプリケーションは
# メールでの申請が必要になりました。そうしていない場合、401エラーが返ります。)
#
# ----
#
# *** Added on 2010-03-10 02:48(UTC) ***
# As of this version, Maraigue, the author, adds specific
# license. The code is distributed under the BSD license.
# See the bottom of the file for the detail of the license.
#
# (*** 2010-03-10 02:48(UTC, 日本時間11:48) 追記 ***
# これ以降のバージョンは、明示的にライセンスを付加します。BSDライセンスとします。
# ライセンスの詳細はこのファイルの末尾をご覧下さい。)
#
# ----
#
# Requesting OAuth access token without browser, only with
# Twitter user name and password (so-called xAuth), by Ruby
#
# (OAuthのアクセストークンを、ブラウザなしで、Twitterの
# ユーザ名およびパスワードのみを用いて取得する(通称:xAuth)
# ためのRubyのコード)
#
# by H.Hiro(Maraigue)
# http://hhiro.net/about/
# http://twitter.com/h_hiro (Feel free to talk in English!)
# main@hhiro.net
#
# ----
#
# OAuth library is required
# http://oauth.rubyforge.org/
#
# This code may not work properly on Ruby 1.9 because the
# OAuth library is coded mainly for Ruby 1.8.
#
# (OAuthライブラリが必要です。このOAuthライブラリはRuby1.8向けの
# ため、1.9では正常に動作しないかもしれません。)
#
# ----
#
# Usage:
# ruby xauth-twitter.rb [1] [2] [3] [4] ([5])
# [1]: Your Twitter Name
# [2]: Your Twitter Password
# [3]: The Consumer Key of Your OAuth Apprication
# [4]: The Consumer Secret of Your OAuth Apprication
# [5]: The Server of the Service (optional: default is 'https://api.twitter.com')
require "kconv"
require "rubygems"
require "oauth"
require "json"
module XAuth
module_function
def retrieve_access_token(username, password, consumer_key, consumer_secret, site = 'https://api.twitter.com')
consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
:site => site
)
# The code below is helped by:
# http://ja.pastebin.ca/1796209
# http://gist.github.com/259989
access_token = consumer.get_access_token(nil, {}, {
:x_auth_mode => "client_auth",
:x_auth_username => username,
:x_auth_password => password,
})
[consumer, access_token]
end
end
if $0 == __FILE__
# retrieving a token
puts "---------- Retrieving an access token... ----------"
consumer, access_token = XAuth.retrieve_access_token(*ARGV)
puts "Consumer key: #{consumer.key}"
puts "Consumer token secret: #{consumer.secret}"
puts "Access token: #{access_token.token}"
puts "Access token secret: #{access_token.secret}"
# example: retrieving timeline
puts "---------- Retrieving the timeline... ----------"
response = access_token.get('/1/statuses/home_timeline.json')
timeline = JSON.load(response.body)
timeline.each do |status|
output = "#{status['user']['screen_name']}: #{status['text']}"
# if encoding of the shell is UTF-8
puts output
# if encoding of the shell is Shift_JIS (in case of Windows[lang=ja])
#puts Kconv.kconv(output, Kconv::SJIS, Kconv::UTF8)
end
end
# Copyright (c) 2010, Maraigue (main@hhiro.net)
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# Neither the name of the Maraigue nor the names of its contributors
# may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment