Azure 了解什么';使用Perl、HTTP和LWP时,我的授权头有问题

Azure 了解什么';使用Perl、HTTP和LWP时,我的授权头有问题,azure,rest,perl,storage,Azure,Rest,Perl,Storage,我有一段PHP代码,用于创建一个Blob存储PUT请求,将其发送到我的Azure存储帐户中的容器。经过重大的修改,我终于得到了正确的头球,一切都很好。不幸的是,我想在其中使用的应用程序是用Perl编写的。所以我认为移植它是一项相对容易的任务。事实证明,这比我预想的要困难得多 我已经比较了PHP代码和Perl代码之间的所有内容(显然不是所有内容,否则会产生异常的效果),但仍然收到与头相关的身份验证错误 PHP脚本使用Curl发出用户代理请求。在我的Perl安装中,我没有可以直接替换的方法。如果没有

我有一段PHP代码,用于创建一个Blob存储PUT请求,将其发送到我的Azure存储帐户中的容器。经过重大的修改,我终于得到了正确的头球,一切都很好。不幸的是,我想在其中使用的应用程序是用Perl编写的。所以我认为移植它是一项相对容易的任务。事实证明,这比我预想的要困难得多

我已经比较了PHP代码和Perl代码之间的所有内容(显然不是所有内容,否则会产生异常的效果),但仍然收到与头相关的身份验证错误

PHP脚本使用Curl发出用户代理请求。在我的Perl安装中,我没有可以直接替换的方法。如果没有本地安装和针对Net::Curl的C编译器,我不确定我能做多少。(可能我遗漏了什么?)由于这两个版本(PHP和Perl)之间的所有内容似乎都是匹配的,即消息、密钥、字符串的编码/解码版本、哈希签名(我硬编码了两个实现之间的验证日期),我不知道还有什么可以尝试。这是第三天,我觉得我可能是在为这个小组已经解决的问题打转

工作正常的PHP代码:


已在注释中解析@格林茨和@Guarav Mantri——你们说得对

  • 我在测试文件中有第二行(一个我没有看到的点)。因此,从文件中读取一行,但使用-s计算整个文件大小将导致不匹配。现在,内容长度的计算是正确的

  • 在签名散列中使用PUT时,我在HTTP::Request-new()调用中盲目地键入了“POST”。哎呀


  • 谢谢各位。不知道如何对两个回答进行投票,因为两个回答都有部分答案。

    my$fdata=将仅读取文件的第一行
    my$fdata=do{local$/;}将读取整个内容。您可能还希望使用句柄,这样就不会对可能的二进制数据进行层转换(例如,Windows上的行尾)。您不需要手动指定内容长度,因为LWP应该根据您提供的内容进行设置。次要的代码样式问题:您应该
    使用警告
    ,您应该使用三个arg open,并且应该使用词法文件句柄而不是全局裸字:
    打开我的$fh',或者使用
    读取二进制文件()
    。@grinz-谢谢。这是一个快速破解,看看我是否可以让它只工作。我用“严格”来防止最严重的错误。这显然不是最干净的代码。:-)谢谢你的评论:我知道这只是一行文字。内容长度是签名哈希的一部分,因此它必须与来自头的LWP计算相匹配(Perl代码的第47行)。然而。。。这确实让我找到了问题所在……没关系,因为这里没有明确的问题答案,只是一个调试过程。这就是为什么我没有提交答案。
    Content-Length header value was wrong, fixed at /usr/share/perl5/vendor_perl/LWP/Protocol/http.pm line 189.
    res: $VAR1 = bless( {
                     '_protocol' => 'HTTP/1.1',
                     '_content' => '<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
    RequestId:62589eac-301e-00bd-3e1e-30c15e000000
    Time:2019-07-01T15:04:08.0485043Z</Message><AuthenticationErrorDetail>The MAC signature found in the HTTP request \'PUUgk2meSoiB9o+inlYomIq96Bf13IdAQoIZ4BSu4sE=\' is not the same as any computed signature. Server used following string to sign: \'POST
    
    
    26
    
    text/plain; charset=UTF-8
    
    
    
    
    
    
    x-ms-blob-type:BlockBlob
    x-ms-date:Mon, 01 Jul 2019 15:04:07 GMT
    x-ms-version:2009-09-19
    /emiliolizardo/containertest/testfile.txt\'.</AuthenticationErrorDetail></Error>',
                     '_rc' => '403',
                     '_headers' => bless( {
                                            'client-response-num' => 1,
                                            'date' => 'Mon, 01 Jul 2019 15:04:07 GMT',
                                            'client-ssl-cert-issuer' => '/C=US/ST=Washington/L=Redmond/O=Microsoft Corporation/OU=Microsoft IT/CN=Microsoft IT TLS CA 4',
                                            'client-ssl-cipher' => 'ECDHE-RSA-AES256-GCM-SHA384',
                                            'client-peer' => '52.239.177.68:443',
                                            'content-length' => '723',
                                            'client-date' => 'Mon, 01 Jul 2019 15:04:08 GMT',
                                            'client-ssl-warning' => 'Peer certificate not verified',
                                            'content-type' => 'application/xml',
                                            'x-ms-request-id' => '62589eac-301e-00bd-3e1e-30c15e000000',
                                            'client-ssl-cert-subject' => '/CN=*.blob.core.windows.net',
                                            'server' => 'Microsoft-HTTPAPI/2.0',
                                            'client-ssl-socket-class' => 'IO::Socket::SSL'
                                          }, 'HTTP::Headers' ),
                     '_msg' => 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.',
                     '_request' => bless( {
                                            '_content' => 'Test file for blob upload
    ',
                                            '_uri' => bless( do{\(my $o = 'https://emiliolizardo.blob.core.windows.net/containertest/testfile.txt')}, 'URI::https' ),
                                            '_headers' => bless( {
                                                                   'user-agent' => 'libwww-perl/5.833',
                                                                   'x-ms-date' => 'Mon, 01 Jul 2019 15:04:07 GMT',
                                                                   'content-type' => 'text/plain; charset=UTF-8',
                                                                   'x-ms-version' => '2009-09-19',
                                                                   'x-ms-blob-type' => 'BlockBlob',
                                                                   'content-length' => 28,
                                                                   'authorization' => 'SharedKey emiliolizardo:PUUgk2meSoiB9o+inlYomIq96Bf13IdAQoIZ4BSu4sE='
                                                                 }, 'HTTP::Headers' ),
                                            '_method' => 'POST',
                                            '_uri_canonical' => $VAR1->{'_request'}{'_uri'}
                                          }, 'HTTP::Request' )
                   }, 'HTTP::Response' );
    $VAR2 = '
    ';