Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl net::oauth返回401 unauthorized,即使curl可以工作_Perl_Oauth - Fatal编程技术网

Perl net::oauth返回401 unauthorized,即使curl可以工作

Perl net::oauth返回401 unauthorized,即使curl可以工作,perl,oauth,Perl,Oauth,我正试图从请求一个令牌,根据文档,它想要的只是一个POST to/+请求令牌,其格式编码值为oauth_consumer_key、oauth_signature和oauth_signature_method。通过curl提供这些项目的效果与预期一致: curl --data "oauth_consumer_key=test-app&oauth_signature=%26&oauth_signature_method=PLAINTEXT" https://launchpad.net

我正试图从请求一个令牌,根据文档,它想要的只是一个POST to/+请求令牌,其格式编码值为oauth_consumer_key、oauth_signature和oauth_signature_method。通过curl提供这些项目的效果与预期一致:

curl --data "oauth_consumer_key=test-app&oauth_signature=%26&oauth_signature_method=PLAINTEXT" https://launchpad.net/+request-token
然而,当我试图通过perl脚本执行此操作时,它给了我一个错误

#!/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,并且必须同时传入帖子和内容:

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

谢谢

很抱歉,我无法从我的平板电脑上验证,但使用最新的Perl,您应该安装并使用

use LWP::Protocol::https;