從您的問題來看,您是否嘗試使用基於PIN或3腿的OAuth尚不清楚。
在基於PIN的OAuth,首先將用戶重定向到授權網址:
use Net::Twitter;
my $nt = Net::Twitter->new(
traits => ['API::RESTv1_1', 'OAuth'],
consumer_key => "YOUR-CONSUMER-KEY",
consumer_secret => "YOUR-CONSUMER-SECRET",
);
my $url = $nt->get_authorization_url();
print "Location: ", $url, "\n\n";
登錄後,用戶會收到必須作爲驗證參數,以獲得訪問令牌傳遞一個PIN碼:
my $pin = 'PIN-NUMBER-PROVIDED-AFTER-LOGIN';
my($access_token, $access_token_secret, $user_id, $screen_name) = $nt->request_access_token(verifier => $pin);
在三足式OAuth,你幾乎以同樣的方式開始,除了呼籲「get_authorization_url」時,必須指定一個回調URL(同您爲您的應用程序註冊):
use Net::Twitter;
my $nt = Net::Twitter->new(
traits => ['API::RESTv1_1', 'OAuth'],
consumer_key => "YOUR-CONSUMER-KEY",
consumer_secret => "YOUR-CONSUMER-SECRET",
);
my $url = $nt->get_authorization_url(callback => 'MY-CALLBACK-URL');
print "Location: ", $url, "\n\n";
這次在登錄後,Twitter會自動重定向到您的回調URL並傳遞兩個查詢字符串參數:oauth_token和oauth_verifier,您應該使用它作爲密鑰/祕密請求令牌以獲取訪問令牌:
use Net::Twitter;
use CGI;
my $cgi = new CGI;
my $oauth_token = $cgi->param('oauth_token');
my $oauth_verifier = $cgi->param('oauth_verifier');
my $nt = Net::Twitter->new(
traits => ['API::RESTv1_1', 'OAuth'],
consumer_key => "YOUR-CONSUMER-KEY",
consumer_secret => "YOUR-CONSUMER-SECRET",
);
$nt->request_token($oauth_token);
$nt->request_token_secret($oauth_verifier);
my($access_token, $access_token_secret, $user_id, $screen_name) = $nt->request_access_token(verifier => $oauth_verifier);
希望這會有所幫助。
如果你的應用程序是一個Mojo應用程序,你可以使用一個插件。 https://metacpan.org/pod/Mojolicious::Plugin::Web::Auth – oalders