AuthenticationFailedServer无法在azure中验证请求

时间:2017-09-13 11:27:55

标签: php azure azure-storage azure-storage-blobs

我正在使用azure rest API来使用PHP获取页面范围。我正在关注其余的API文档here

我无法验证它所说的其余API

  

HTTP请求中找到的MAC签名' 6fsdfhyuiz78'不是   与任何计算签名相同。服务器使用字符串后签名   GET

我使用以下代码来获取页面范围。在传递请求标头以生成字符串以签署身份验证签名时,我遗漏了一些内容。如果有人可以帮助我,那就太好了。

$date = gmdate('D, d M Y H:i:s \G\M\T');
$account_name = "account name";
$containername = "container name";
$blob = "blobname.vhd";

$mypremiumdisk_ss1 = "2017-09-12V09:15:43.00001Z";
$mypremiumdisk_ss2 = "2017-09-12V09:55:47.00002Z";

$account_key = "account key";

$canonicalizedHeaders  = "x-ms-date:$date\nx-ms-version:2015-07-08";
$canonicalizedResource = "/$account_name/$containername/$blob\ncomp:pagelist\nsnapshot:$mypremiumdisk_ss1\nprevsnapshot:$mypremiumdisk_ss2";

$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';
echo $url = $endpoint.'/'.$containername.'/'.$blob.'?comp=pagelist&snapshot='.$mypremiumdisk_ss1.'&prevsnapshot='.$mypremiumdisk_ss2; 

$headers = [
    "x-ms-date:{$date}",
    'x-ms-version:2015-07-08',
    'Accept:application/json;odata=nometadata',
    'Accept-Charset:UTF-8',
    "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);

1 个答案:

答案 0 :(得分:0)

我注意到的一些事情:

$mypremiumdisk_ss1 = "2017-09-12V09:15:43.00001Z";
$mypremiumdisk_ss2 = "2017-09-12V09:55:47.00002Z";

请将日期/时间值中的V更改为T

$canonicalizedResource = "/$account_name/$containername/$blob\ncomp:pagelist\nsnapshot:$mypremiumdisk_ss1\nprevsnapshot:$mypremiumdisk_ss2";

根据documentationcanonicalizedResource应包含按字母顺序排序的查询参数。请将以上行更改为:

$canonicalizedResource = "/$account_name/$containername/$blob\ncomp:pagelist\nprevsnapshot:$mypremiumdisk_ss2\nsnapshot:$mypremiumdisk_ss1";