Perlã§Twitterã®OAuthã使ããµã³ãã«
èªåç¨ã¡ã¢ã§ãã
ãããããªãµã¤ããåèã«ãã¾ããããå®å
¨ãªã³ã¼ããè¦ã¤ãããªãã£ãã®ã§ã¨ãããããªã³ã¯ã¯çç¥ãã¾ãã
Twitterã®OAuthClientãä¸è¨URLããè¨å®
http://twitter.com/oauth_clients
consumer_keyã¨consumer_secretãåå¾ããã
ããã¾ããªæµã
- index.html ãããªã³ã¯ã§ sample.pl ã«ã¢ã¯ã»ã¹ããTwitterã®OAuth許å¯ç¨ã®ãã¼ã¸ã¸ãªãã¤ã¬ã¯ãã
- OAuth許å¯ç¨ã®ãã¼ã¸ã§è¨±å¦ãè¨å®å¾ãsample.pl ã§è¨å®ãã callback_url (ããéè¦)ã¸ãªãã¤ã¬ã¯ãããã(ãã®å ´åã¯callback.pl)ã
- callback.pl 㧠oauth_token 㨠oauth_verifier ãåå¾ããupdateã試ã¿ãã
- æåã§ããã°ããã¤ã¼ãå ã¸ã失æã§ããã°Twitterããããã¼ã¸ã«ãªãã¤ã¬ã¯ã
ã³ã¼ãä¾
sample.pl
#!/usr/bin/perl use strict; use warnings; use utf8; use lib '/home/puny/local/lib/perl5'; use OAuth::Lite::Consumer; use URI; use CGI; use CGI::Carp qw/fatalsToBrowser/; my $q = CGI->new(); my $consumer = OAuth::Lite::Consumer->new( consumer_key => 'YOUR_CONSUMER_KEY', consumer_secret => 'YOUR_CONSUMER_SECRET', callback_url => 'http://linknode.net/jabox/callback.pl', 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 $request_token = $consumer->get_request_token(); my $uri = URI->new($consumer->{authorize_path}); $uri->query( $consumer->gen_auth_query("GET", 'http://twitter.com', $request_token) ); print $q->redirect($uri->as_string); exit;
callback.pl
#!/usr/bin/perl use strict; use warnings; use utf8; use lib '/home/puny/local/lib/perl5'; use OAuth::Lite::Consumer; use Encode; use CGI; use CGI::Carp qw/fatalsToBrowser/; use URI; my $q = CGI->new(); *OAuth::Lite::Util::encode_param = sub { my $param = shift; URI::Escape::uri_escape_utf8($param, '^\w.~-'); }; my $consumer = OAuth::Lite::Consumer->new( consumer_key => 'YOUR_CONSUMER_KEY', consumer_secret => 'YOUR_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 $param_oauth_token = $q->param('oauth_token'); my $param_oauth_verifier = $q->param('oauth_verifier'); my $access_token = $consumer->get_access_token( token => $param_oauth_token, verifier => $param_oauth_verifier, ); my $res = $consumer->request( method => 'POST', url => q{http://twitter.com/statuses/update.xml}, token => $access_token, params => { status => scalar localtime, token => $access_token, }, ); if ($res->is_success) { use XML::Simple; my $status = XMLin($res->decoded_content); print $q->redirect( "http://twitter.com/" . $status->{user}->{screen_name} . '/status/' . $status->{id} ); } else { print $q->redirect('http://twitter.com'); } exit;