php - AuthenticationFailedServer 无法对 azure 中的请求进行身份验证

标签 php azure azure-storage azure-blob-storage

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

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

The MAC signature found in the HTTP request '6fsdfhyuiz78' is not the same as any computed signature. Server used following string to sign GET

我使用下面的代码来获取页面范围。在传递请求 header 以生成用于签署身份验证签名的字符串时,我在这里错过了一些内容。如果有人能帮助我解决这个问题,那就太好了。

$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);

最佳答案

我注意到的一些事情:

$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";

基于documentation , canonicalizedResource 应包含按字母顺序排序的查询参数。请将以上行更改为:

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

关于php - AuthenticationFailedServer 无法对 azure 中的请求进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46196352/

相关文章:

php - 在 PHP 中合并两个 sql 查询的结果

linux - 我的 azure functionapp 可以在 Linux 上运行吗?

azure - C# Azure 存储从一个 blob 复制文件到另一个 blob

azure - 将 Azure Data Lake Storage 挂载到 Windows VM 文件系统

php - 构造父类时设置对象的子类(动态子类)

php - 如何在 PHP 和 JS 中并发 session 之间进行通信?

python - 如何在 Python 中从 Azure Function 调用 Cosmos DB 存储过程?

sql - 从 URL(Azure 存储)还原数据库失败,没有更多详细信息

php - 在 jquery div show() 中提交表单 $_SERVER ['PHP_SELF' ]

azure - 并行调用数百个 azure 函数