【发布时间】:2020-12-29 23:59:41
【问题描述】:
我正在尝试通过我的 Perl 脚本通过 REST API 在 Azure 表中进行授权。根据MSDN documents 并基于我对类似任务的了解(我为 Blob 服务创建了一个类似的脚本并且效果很好)我开发了一个简单的脚本:
use strict;
use warnings;
use Digest::SHA qw(hmac_sha256_base64);
use HTTP::Date;
use HTTP::Request;
use LWP::UserAgent;
use MIME::Base64;
my $account = 'myaccount';
my $key = 'Nf2b/ZSY+a...7ZT0Q==';
my $decoded_key = decode_base64( $key );
my $uri = "https://$account.table.core.windows.net/?restype=service&comp=properties";
my $method = 'GET';
my $req = HTTP::Request->new($method, $uri);
my $date = time2str();
my $canonicalized_resource = "/$account/\nrestype:service\ncomp:properties";
my $string_to_sign =
"$method\n" .
"\n" . # content-md5
"\n" . # content-type
"$date\n".
$canonicalized_resource;
my $sig = hmac_sha256_base64($string_to_sign, $decoded_key );
$sig .= '=' x (4 - (length($sig) % 4));
$req->authorization("SharedKey $account:$sig");
$req->header('x-ms-version', '2019-02-02');
$req->header('x-ms-date', $date);
$req->date($date);
my $ua = LWP::UserAgent->new();
my $resp = $ua->request($req);
unless($resp->is_success){
die "Request failed: " . $resp->status_line . ". Signed string: " . $string_to_sign;
}
任务很简单:通过获取Table service properties 检查我的代码。不幸的是,它不起作用!我得到:
请求失败:403 服务器未能验证请求。确保 Authorization 标头的值格式正确,包括签名。
【问题讨论】:
-
我不熟悉 Azure,但根据docs 看来
$string_to_sign缺少一些项目(如内容编码)..