2010/02/17

TwitterのBasic認証APIは6月で廃止される予定なのですが、OAuthという認証方法はブラウザを起動してユーザに認証して貰わなければなりません。一見flickrアプリケーションの様な認証方法を想定しますが、OAuthはflickr認証の様にサーバから貰ったトークンをブラウザから渡して認証させる様な物ではありません。
今回OAuthの問題を解決すべくOAuthを拡張した認証方式であるxAuthが取り入れられました。
詳しくはAPIドキュメントか以下のサイトが分かりやすいかと思います。
s-take Blog.: Twitterによる簡易版OAuth: "xAuth"

従来のOAuth認証ではまずアプリケーション(OAuthコンシューマ)がTwitterに接続してRequest Tokenを取得し、認証画面を開いてRequest Tokenを承認させ、承認されたRequest Tokenを使ってAccess TokenとToken Secretを取得することによって各APIにアクセスできるようになります。しかしこれはアプリケーション側の実装が複雑になる上、デスクトップアプリケーションの場合はわざわざWebブラウザへ切り替えなければならず(ブラウザを内包するものもありますが)、ユーザにとっても面倒なものです。

http://s-take.blogspot.com/2010/02/twitteroauth-xauth.html
the.hackerConundrum: Sneak peek at Twitter's browserless OAuth credentials exchange method

Over the past couple of months the Twitter API Google Group has been overflowing with more and more disgruntled developers complaining about lack of bug fixes, slow rollout of promised features, no mobile interface for OAuth, etc. (The list goes on and on) Well I'm happy to say Twitter appears to be almost done with one much requested feature: browserless OAuth credentials exchange. It was hinted that Seesmic Look was using said exchange so today I took a peek at how Look worked behind the scenes.

http://the.hackerconundrum.com/2010/02/sneak-peek-at-twitters-browserless.html
実装だと
Maraigue風。:[Ruby][Twitter] OAuthのアクセストークンを、ブラウザなしで、Twitterのユーザ名およびパスワードのみを用いて取得する(通称:xAuth)ためのRubyのコード
http://blog.livedoor.jp/maraigue/archives/1109122.html
とか
pastebin - 誰か - post number 1796209
http://ja.pastebin.ca/1796209
あとOAuthな話ですが
あまやどり: OAuthで認証してTwitterでつぶやいてみた
http://petitbanca.blogspot.com/2009/11/oauthtwitter.html
あたりが参考になります。今日はpythonを使ってxAuthするサンプルを書いてみました。pythonにも元々oauthライブラリはあるのですが、今回は分かりやすく使わず書いてみました。
以下ソース。
from pit import Pit
from random import getrandbits
from time import time
import hmac, hashlib
import sys
import urllib
import urllib2
import urlparse

consumer_key = 'YOUR-CONSUMER-KEY'
consumer_secret = 'YOUR-CONSUMER-SECRET'
user = Pit.get('twitter.com', {'require' : {
  'username' : 'your username in twitter.com',
  'password' : 'your password in twitter.com',
}})
message = sys.argv[1]
access_url = 'https://api.twitter.com/oauth/access_token'
post_url = 'http://twitter.com/statuses/update.json'

# build parameter to get access token
params = {
  'oauth_consumer_key' : consumer_key,
  'oauth_signature_method' : 'HMAC-SHA1',
  'oauth_timestamp' : str(int(time())),
  'oauth_nonce' : str(getrandbits(64)),
  'oauth_version' : '1.0',
  'x_auth_mode' : 'client_auth',
  'x_auth_username' : user['username'],
  'x_auth_password' : user['password'],
}
params['oauth_signature'] = hmac.new(
  '%s&%s' % (consumer_secret, ''),
  '&'.join([
      'POST',
      urllib.quote(access_url, ''),
      urllib.quote('&'.join(['%s=%s' % (x, params[x])
          for x in sorted(params)]), '')
  ]),
  hashlib.sha1).digest().encode('base64').strip()

# get access token
req = urllib2.Request(access_url, data = urllib.urlencode(params))
res = urllib2.urlopen(req)
token = urlparse.parse_qs(res.read())
token_key = token['oauth_token'][0]
token_secret = token['oauth_token_secret'][0]

# build parameters to post
params = {
  'oauth_consumer_key' : consumer_key,
  'oauth_signature_method' : 'HMAC-SHA1',
  'oauth_timestamp' : str(int(time())),
  'oauth_nonce' : str(getrandbits(64)),
  'oauth_version' : '1.0',
  'oauth_token' : token_key,
}
params['status'] = urllib.quote(message, '')
params['oauth_signature'] = hmac.new(
  '%s&%s' % (consumer_secret, token_secret),
  '&'.join([
      'POST',
      urllib.quote(post_url, ''),
      urllib.quote('&'.join(['%s=%s' % (x, params[x])
          for x in sorted(params)]), '')
  ]),
  hashlib.sha1).digest().encode('base64').strip()
del params['status']

# post with oauth token
req = urllib2.Request(post_url, data = urllib.urlencode(params))
req.add_data(urllib.urlencode({'status' : message}))
req.add_header('Authorization', 'OAuth %s' % ', '.join(
  ['%s="%s"' % (x, urllib.quote(params[x], '')) for x in params]))

# done!
print urllib2.urlopen(req).read()
生の処理で書いてあるので、oauthライブラリに依存させたくない様な移植には参考になるかもしれません。
ところで今回GtkTwitterというC言語で書いたTwitterクライアントのBasic認証を止めようと思っていてこの件を調べ始めたのですが, どうやらTwitterに登録するOAuthアプリケーションには「Twitter」という文言を使ってはいけない事が今日分かりました。まぁGtkTwitterはクライアントアプリが名前登録出来た頃に書いた物なので、あの頃はOKだったのかも知れません。
しかしまぁ...どうせぃっちゅうねん!

どうしましょ。Gtkほにゃらら...何がいいやろ。困った。
Posted at 09:44 | WriteBacks () | Edit
Edit this entry...

wikieditish message: Ready to edit this entry.






















A quick preview will be rendered here when you click "Preview" button.