Sign in with Twitter を実装する

最近、ユーザー登録とログインを管理するのが面倒だったりするので、
いっそ、Twitterの情報を利用すればいいのではないかと実験しました。


今回は依存のかなり多い、Net::Twitterは使わずに、
OAuth::Lite::Consumerを使う事にしました。


仕組みとしては下記の画像のようになります。
http://a0.twimg.com/images/dev/cms/oauth_sign_in_with_twitter.png:image=http://a0.twimg.com/images/dev/cms/oauth_sign_in_with_twitter.png:w480

つまり、一旦、Twitterに飛んで承認すれば、
決められたURLに戻ってきて、そこでTwitterの情報を利用することができます。


以下のコードはTwitterの承認ページへ導く処理です。
Twitter / アプリより、consumer_key と consumer_secret を取得する必要があります。
・request.cgi

#!/usr/bin/env perl

use strict;
use OAuth::Lite::Consumer;
use URI;
use CGI;

my $q = CGI->new();

my $consumer_key = "your_consumer_key";
my $consumer_secret = "your_consumer_secret";
my $consumer = OAuth::Lite::Consumer->new(
    consumer_key => $consumer_key,
    consumer_secret => $consumer_secret,
    site => 'http://twitter.com/',
    request_token_path => 'http://twitter.com/oauth/request_token',
    access_token_path => 'http://twitter.com/oauth/access_token',
    authorize_path => 'http://twitter.com/oauth/authorize',
    # 承認後戻ってくるURL
    callback_url => 'http://~/callback.cgi',
    );
    
my $request_token = $consumer->get_request_token();
my $uri = URI->new('http://twitter.com/oauth/authenticate');
$uri->query(
    $consumer->gen_auth_query('GET','http://twitter.com',$request_token)
    );
print $q->redirect($uri->as_string);

1;


承認先のURLを

http://twitter.com/oauth/authenticate

にすることで、承認されていれば使う度に承認する必要がなくなるそうです。


oauth_token と oauth_verifier が付いた状態で戻ってきます。
以下は戻ってきて、Twitterの情報を取得する処理のコードです。
※上で指定したURLに配置します。
・callback.cgi

#!/usr/bin/env perl

use strict;
use OAuth::Lite::Consumer;
use URI;
use XML::Simple;

my $consumer_key = "your_consumer_key";
my $consumer_secret = "your_consumer_secret";
my $consumer = OAuth::Lite::Consumer->new(
    consumer_key => $consumer_key,
    consumer_secret => $consumer_secret,
    site => 'http://twitter.com/',
    request_token_path => 'http://twitter.com/oauth/request_token',
    access_token_path => 'http://twitter.com/oauth/access_token',
    authorize_path => 'http://twitter.com/oauth/authorize',
    );
my $oauth_token = $q->param('oauth_token');
my $oauth_verifier = $q->param('oauth_verifier');

my $access_token = $consumer->get_access_token(
    token => $oauth_token,
    verifier => $oauth_verifier
    );

# Twitter APIを使用します。
my $res = $consumer->request(
    method => 'GET',
    url => q{http://api.twitter.com/1/account/verify_credentials.xml},
    token => $access_token,
    );

if($res->is_success){
    my $xml = XMLin($res->decoded_content);
    my $id = $xml->{'screen_name'};
    print "Content-Type: text/html\n\n";
    print "Twitter id: ".$id."\n";
}else{
    print $q->redirect('http://~');
}

1;

これでTwitterのidを取得することができ、
それをWebアプリケーションでも使用することができます。


戻ってきた後のTwitter APIの仕様は以下に詳細に書かれています。

Page not found | Twitter Developers


ref:

Sign in with Twitter | Twitter Developers
PHPで「Sign in with Twitter」を実装する方法 - 頭ん中
PerlでTwitterのOAuthを使うサンプル - punitan (a.k.a. punytan) のメモ
PerlでOAuthしてみた - jitsu102の日記
OAuth::Lite::Consumerのget_request_token時にエラー - 徹夜族