Twitterã®OAuthèªè¨¼ãªããããæ¸ãã®ã¯ç°¡å
Twitterããããæ¸ããã¨èªä½ã¯ã¨ã¦ãç°¡åã ããã©ãOAuthèªè¨¼ï¼é¢åããï¼ã£ã¦äººãããããªã®ã§ãå ¨ç¶ãããªãã¨ããªãããã¨è¨ã話ãæ¸ãã¦ãããã¨æããããããæ¢ãã¨ãããã§ãåºã¦ããæ å ±ã§ã¯ããã®ã ããã©ãAPIã¸ã®ã¢ã¯ã»ã¹ãã©ã¤ãã©ãªã«ä»»ãã¦ããã®ãªãã°ãè¦ããã«åé¡ã¯ã©ããã£ã¦Access Tokenãåå¾ãããã£ã¦ãã¨ã ãã«ãªãã¨æãã®ã§ãããã«ã¤ãã¦ã
ããã§ã¯Perlã®Net::Twitterã使ã£ã¦æ¸ãããã©ãã©ããªã©ã¤ãã©ãªã§ããããã¦å¤ãããªãã ããã¨æ¨æ¸¬ãããã
Net::Twitterã§OAuth
Net::Twitter::Role::OAuthのperldocè¦ã¦ï¼ã¨è¨ã£ããå ãåããªãã®ã§ï¼æ¬å½ã«ããã ãã®è©±ã ããã©ï¼ããµã³ãã«ã³ã¼ãã§ããããperldocã«æ¸ãã¦ããã
use Net::Twitter; my $nt = Net::Twitter->new( traits => ['API::REST', 'OAuth'], consumer_key => '...', consumer_secret => '...', ); $nt->access_token('...'); $nt->access_token_secret('...'); # ãã¨ã¯ãã¤ãéãã§
ãã¦ãããã§consumer_keyã¨consumer_secretã¯ãdev.twitter.comããç»é²åºæ¥ãã¢ããªã±ã¼ã·ã§ã³ã®Consumer Key/Secretãªã®ã§çåã¯ãªãã ããï¼ããããªãã¯ã©ã¤ã¢ã³ãã¢ããªã¨ãã¦ç»é²ãã¦ãããã¨ï¼ãåé¡ã¯ãaccess_tokenã¨access_token_secretãã©ããã£ã¦åå¾ããã®ãï¼é¢åããããï¼ï¼ã¨ããã¨ããã
Access Tokenãåå¾ãã
ãããNet::Twitter::Role::OAuthã®ãµã³ãã«ã³ã¼ããè¦ãã¨æ¸ãã¦ããã®ã ããã©ãã¡ãã£ã¨ã ãæ¸ãç´ãã¦ã¿ãã
#!perl use strict; use warnings; use Net::Twitter; print "Consumer Key:\n"; my $consumer_key = <STDIN>; chomp $consumer_key; print "Consumer Secret:\n"; my $consumer_secret = <STDIN>; chomp $consumer_secret; print <<EOD; --- CONSUMER KEY AND SECRET --- consumer_key : $consumer_key consumer_secret : $consumer_secret EOD my $nt = Net::Twitter->new( traits => ['API::REST', 'OAuth'], consumer_key => $consumer_key, consumer_secret => $consumer_secret, ); print "Authorize this app at ", $nt->get_authorization_url, " and enter the PIN#\n"; my $pin = <STDIN>; chomp $pin; my($access_token, $access_token_secret, $user_id, $screen_name) = $nt->request_access_token(verifier => $pin); print <<EOD; --- ACCESS TOKEN --- access_token : $access_token access_token_secret : $access_token_secret user_id : $user_id screen_name : $screen_name EOD
ãããå®è¡ããã¨ãã¾ãã¯consumer key/secretãèããããã¢ããªã±ã¼ã·ã§ã³ç»é²æã®ãã®ãçããã ããªã®ã§ãã³ããã§ãªãã¨ããªãã ããã
ãã®ã¾ã¾é²ãã¨ãèªè¨¼ç¨URIã表示ããããããã«ããããåããããã¢ã«ã¦ã³ãã§ãã°ã¤ã³ããç¶æ ã®ãã©ã¦ã¶ã§ã¢ã¯ã»ã¹ãããããããã¨ãã¤ãã®ã許å¯ããæå¦ãã®ãã¼ã¸ã«ãªãã®ã§ãã許å¯ããã¦PINãå¾ãã
ã¹ã¯ãªããã«æ»ãPINããã¼ã¹ããã¦å®è¡ãç¶ããã°ãç¡äºAccess Tokenã¨Access Token Secretãå¾ãããã¯ãã ããã¨ã¯ãããconfigã«æ¸ããªãbotã«ãã¼ãã³ã¼ããããªãããã°ããã®å ã¯ããã¾ã§éãã®ããæ¹ã§æ¸ãã
ã¾ã¨ã
OAuthãé¢åã¨è¨ã£ã¦ããã®ã¯ãèªè¨¼å®è£ ä¸ã®è«¸åé¡ãNet::Twitterãªã©ã®ã©ã¤ãã©ãªã«ä»»ãããªãã°ãAccess Tokenã®åå¾ãã§ã¼ãºãé¢åã¨è¨ã話ã§ãããããã¯ä¸è¨ã®ãããªå°ç¨ã®åå¾ã¹ã¯ãªãããç¨æãã¦ãããã¨ã§ç°¡åã«è§£æ±ºåºæ¥ãã
ãã¾ã
ã¡ãªã¿ã«ãã¢ããªã±ã¼ã·ã§ã³ç»é²ãããã¢ã«ã¦ã³ãã¨ããããåããã¢ã«ã¦ã³ããåãå ´åã¯ãdev.twitter.comã«æ¸ãã¦ããAccess Tokenã使ãã°è¯ã*1ã®ã§ãä¸è¨ã®æä½ããä¸è¦ã§ãã
*1:å³ã«ã©ã ã®My Access Tokenããã¢ã¯ã»ã¹åºæ¥ã¾ã