HTTP请求中找到的MAC签名与使用php进行的任何计算的签名天蓝色集成都不相同 [英] The MAC signature found in the HTTP request is not the same as any computed signature azure integration using php

查看:79
本文介绍了HTTP请求中找到的MAC签名与使用php进行的任何计算的签名天蓝色集成都不相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用azure rest API列出blob.我正在使用以下代码使用curl和php列出blob,看起来生成的auth签名是错误的,任何人都可以帮助我解决授权问题.

I am trying to list blobs using azure rest API. I am using the below code to list blobs using curl and php looks like the auth signature generated is wrong could anyone help me out in resolving the authorization issue.

$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'.

推荐答案

我已将您的代码更改为以下内容,然后使其正常工作.

I have changed your code to something as below, then got it worked.

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

这篇关于HTTP请求中找到的MAC签名与使用php进行的任何计算的签名天蓝色集成都不相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆