php - HTTP 请求中找到的 MAC 签名与使用 php 的任何计算签名 azure 集成不同

标签 php azure azure-blob-storage

我正在尝试使用 Azure Rest API 列出 Blob。我使用下面的代码使用curl和php列出blob,看起来生成的身份验证签名是错误的,任何人都可以帮助我解决授权问题。

$date = gmdate('D, d M Y H:i:s \G\M\T');
$account_name = "xyz";
$containername = "abc";
$account_key = "asdf";

$stringtosign = "GET\n\n\n$date\n/$account_name/$containername()";
$signature = 'SharedKey'.' '.$account_name.':'.base64_encode(hash_hmac('sha256', $stringtosign, base64_decode($account_key),true));

$endpoint = 'https://'.$account_name.'.blob.core.windows.net';
$url = $endpoint.'/'.$containername.'?restype=container&comp=list'; 

$headers = [
    "x-ms-date:{$date}",
    'x-ms-version:2014-02-14',
    'Accept:application/json;odata=nometadata',
    "Authorization:{$signature}"
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);        
echo '<pre>';print_r($response);

我收到以下错误

AuthenticationFailedServer failed to authenticate the request. Make sure the 

value of Authorization header is formed correctly including the signature.
RequestId:sdf4b5-0341-043559-2sdf53-b3sdf000
Time:2017-04-12T06:10:18.8696178ZThe MAC signature found in the HTTP request 'sfdf98i8p7f1QiJwszds' is not the same as any computed signature. Server used following string to sign: 'GET
x-ms-date:Wed, 12 Apr 2017 06:10:17 GMT
x-ms-version:2014-02-14
/abc/xyz
comp:list
restype:container'.

最佳答案

我已将您的代码更改为如下所示,然后它就可以工作了。

<?php

$date = gmdate('D, d M Y H:i:s \G\M\T');
$account_name = "xyz";
$containername = "abc";
$account_key = "asdf";

$canonicalizedHeaders  = "x-ms-date:$date\nx-ms-version:2014-02-14";
$canonicalizedResource = "/$account_name/$containername\ncomp:list\nrestype:container";

$arraysign = array();
$arraysign[] = 'GET';                     /*HTTP Verb*/  
$arraysign[] = '';                        /*Content-Encoding*/  
$arraysign[] = '';                        /*Content-Language*/  
$arraysign[] = '';                        /*Content-Length (include value when zero)*/  
$arraysign[] = '';                        /*Content-MD5*/  
$arraysign[] = '';                        /*Content-Type*/  
$arraysign[] = '';                        /*Date*/  
$arraysign[] = '';                        /*If-Modified-Since */  
$arraysign[] = '';                        /*If-Match*/  
$arraysign[] = '';                        /*If-None-Match*/  
$arraysign[] = '';                        /*If-Unmodified-Since*/  
$arraysign[] = '';                        /*Range*/  
$arraysign[] = $canonicalizedHeaders;     /*CanonicalizedHeaders*/
$arraysign[] = $canonicalizedResource;    /*CanonicalizedResource*/

$stringtosign = implode("\n", $arraysign);

$signature = 'SharedKey'.' '.$account_name.':'.base64_encode(hash_hmac('sha256', $stringtosign, base64_decode($account_key), true));

$endpoint = 'https://'.$account_name.'.blob.core.windows.net';
$url = $endpoint.'/'.$containername.'?restype=container&comp=list'; 

$headers = [
    "x-ms-date:{$date}",
    'x-ms-version:2014-02-14',
    'Accept:application/json;odata=nometadata',
    "Authorization:{$signature}"
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);        
echo '<pre>';print_r($response);

关于php - HTTP 请求中找到的 MAC 签名与使用 php 的任何计算签名 azure 集成不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43361709/

相关文章:

javascript - 如何在谷歌地图中添加多个标记

javascript - WordPress + Vue.js : Contact Forms

python - 将流数据从 Azure Blob 存储读取到 Databricks 中

azure - 将 PowerShell 对象数据记录到 Azure blob,因为 CSV 缺少换行符

azure - 使用 Azure Storage Rest API 创建文件夹而不创建默认 blob 文件

c# - 尽管容器、路径和 uri 正确,Azure 存储 Blob GetBlobs 方法仍失败

php - 如何将数据数组从模型传递到 Controller ,然后再传递回模型?

php - 如何在使用PHP提交表单后返回页面

Azure函数计时器触发器在部署时不触发

azure - 我可以在 BotBuilder 中使用 PubNub 而不是 DirectLine 吗?