我尝试了两种不同的方法来进行 vimeo 高级 api 身份验证。请在下面找到:-
第一种方法:
#!perl
use strict;
use warnings;
use Digest::HMAC_SHA1;
use Data::Dump qw /dump/;
use URI::Escape;
use Net::OAuth;
use MIME::Base64;
use LWP::UserAgent;
# Authorize a user
my $consumer_key = "CONSUMERKEY";
my $secret = "SECRET";
my $method = "vimeo.videos.search" ;
my $oauth_nonce = int( rand( 2**32 ) ) ;
my $timestamp = time ;
my $query = "happy" ;
my $url = "http://vimeo.com/api/rest/v2/" ;
my $str = "method=$method&oauth_consumer_key=$consumer_key&oauth_nonce=$oauth_nonce&oauth_signature_method=HMAC-SHA1&oauth_timestamp=$timestamp&oauth_version=1.0&query=$query";
$str = uri_escape( $str) ;
$url = uri_escape( $url) ;
my $secret_key = $secret . '&';
my $base_str = "GET" . "&" . $url . "&" . $str ;
my $hmac = Digest::HMAC_SHA1->new( $secret_key ) ;
$hmac->add($base_str) ;
my $oauth_signature = $hmac->b64digest ;
$oauth_signature = encode_base64($oauth_signature );
chomp $oauth_signature;
$oauth_signature = uri_escape( $oauth_signature );
my $v_search_url = "http://vimeo.com/api/rest/v2? method=$method&oauth_consumer_key=$key&oauth_nonce=$oauth_nonce&oauth_signature_method=HMAC-SHA1&oauth_timestamp=$timestamp&oauth_version=1.0&oauth_signature=$oauth_signature&query=$query" ;
my $browser = LWP::UserAgent->new;
my $res = $browser->get( $v_search_url );
print $res->content;
The response content throws following error:-
<?xml version="1.0" encoding="UTF-8"?>
<rsp generated_in="0.0024" stat="fail">
<err code="401" expl="The consumer key passed was not valid." msg="Invalid consumer key"/>
</rsp>
上面使用的消费者密钥是有效的(虽然在顶部没有共享)。直接在 mozilla 浏览器上点击上述搜索 url 会引发此错误:“传递的 oauth_signature 无效。” 请让我知道代码到底在哪里出错。我遵循了以下提供的 oauth 规范:http: //vimeo.com/api/docs/oauth
第二种方法:
#!perl
use strict;
use warnings;
use Digest::HMAC_SHA1;
use Data::Dump qw /dump/;
use URI::Escape;
use Net::OAuth;
use Net::OAuth::RequestTokenRequest;
use MIME::Base64;
use LWP::UserAgent;
# Authorize a user
my $consumer_key = "CONSUMERKEY";
my $secret = "SECRET";
my $method = "vimeo.videos.search" ;
my $oauth_nonce = int( rand( 2**32 ) ) ;
my $timestamp = time ;
my $query = "happy" ;
my $url = "http://vimeo.com/oauth/request_token";
my $request = Net::OAuth::RequestTokenRequest->new(
consumer_key => $consumer_key,
consumer_secret => $secret,
request_url => $url,
request_method => 'GET',
signature_method => 'HMAC-SHA1',
timestamp => $timestamp,
nonce => $oauth_nonce,
);
die "COULDN'T GET REQUEST SIGN! Check parameters.\n" unless $request->sign;
die "COULDN'T VERIFY! Check OAuth parameters.\n" unless $request->verify;
my $browser = LWP::UserAgent->new;
my $post_body = $request->to_post_body;
$post_body = "oauth_callback=oob&" . $post_body;
my $post_url = $url . '/?' . $post_body;
my $res = $browser->get( $post_url );
die $res->status_line unless ($res->is_success);
print $res->content;
此方法也因抛出上述错误“401 Unauthorized”而失败,并且在浏览器上点击 url 时会抛出相同的“无效的 oauth 签名错误”。由于这个人在 Net::Oauth 的第一步抛出错误,我无法继续进行。任何输入都受到高度赞赏。