net :: oauth即使卷曲有效,也会返回401

时间:2013-02-26 22:46:05

标签: perl oauth

我正在尝试从https://launchpad.net请求令牌,根据文档,它想要的是POST到/ +请求令牌,其格式为oauth_consumer_key,oauth_signature和oauth_signature_method。通过卷曲提供这些项目按预期工作:

curl --data "oauth_consumer_key=test-app&oauth_signature=%26&oauth_signature_method=PLAINTEXT" https://launchpad.net/+request-token

但是,当我尝试通过我的perl脚本执行此操作时,它会给我一个未经授权的401错误。

#!/usr/bin/env perl

use strict;
use YAML qw(DumpFile);
use Log::Log4perl qw(:easy);
use LWP::UserAgent;
use Net::OAuth;
$Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
use HTTP::Request::Common;
use Data::Dumper;
use Browser::Open qw(open_browser);

my $ua = LWP::UserAgent->new;

my ($home)           = glob '~';
my $cfg              = "$home/.lp-auth.yml";
my $access_token_url = q[https://launchpad.net/+access-token];
my $authorize_path   = q[https://launchpad.net/+authorize-token];

sub consumer_key { 'lp-ua-browser' }
sub request_url {"https://launchpad.net/+request-token"}

my $request = Net::OAuth->request('consumer')->new(
    consumer_key     => consumer_key(),
    consumer_secret  => '',
    request_url      => request_url(),
    request_method   => 'POST',
    signature_method => 'PLAINTEXT',
    timestamp        => time,
    nonce            => nonce(),
);

$request->sign;
print $request->to_url;

my $res = $ua->request(POST $request->to_url, Content $request->to_post_body);
my $token;
my $token_secret;
print Dumper($res);
if ($res->is_success) {
    my $response =
      Net::OAuth->response('request token')->from_post_body($res->content);
    $token        = $response->token;
    $token_secret = $response->token_secret;
    print "request token ",       $token,        "\n";
    print "request token secret", $token_secret, "\n";
    open_browser($authorize_path . "?oauth_token=" . $token);
}
else {
    die "something broke ($!)";
}

我尝试使用$request->sign和没有它,因为我不认为在请求令牌阶段需要它。无论如何,对此有任何帮助将不胜感激。

更新,切换到LWP :: UserAgent并且必须传递POST和内容:

my $res = $ua->request(POST $request->to_url, Content $request->to_post_body);

由于

1 个答案:

答案 0 :(得分:0)

抱歉,我无法通过平板电脑验证,但是最近的Perl应该安装并使用

use LWP::Protocol::https;

http://blogs.perl.org/users/brian_d_foy/2011/07/now-you-need-lwpprotocolhttps.html