REST API in Windows Azure


REST API in Windows Azure

我正在尝试连接到Windows Azure REST API,但总是得到这样的问题:

The MAC signature found in the HTTP request '...' is not the same as any computed signature.

我不能得到正确的签名。我也尝试了Objective-C和Windows Azure REST API的不同方法,但总是得到同样的错误。PHP上有完整的清单:

function send_request($url, $headers)
{
    if ($curl = curl_init())
    {
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        return curl_exec($curl);
        curl_close($curl);
    }
    return false;
}
function encode_string_to_sign($string_to_sign, $key)
{
    $hash = hash_hmac("sha256", $string_to_sign, $key, true);
    $signature = base64_encode($hash);
    return $signature;
}
header("Content-Type: application/xhtml+xml");
// GET Container Metadata
$url = "http://snip.blob.core.windows.net/elements?restype=container&comp=metadata";
$access_key_1 = "...";
$access_key_2 = "...";
$current_date = "Thu, 28 Feb 2013 21:10:00 GMT";
$canonicalized_headers = "x-ms-date:$current_date'nx-ms-version:2009-09-19";
$canonicalized_resource = "/snip/elements'ncomp:metadata'nrestype:container";
$string_to_sign = "GET'n'n'n'n'n'n'n'n'n'n'n'n" . $canonicalized_headers . "'n" . $canonicalized_resource;
$signature = utf8_encode(encode_string_to_sign($string_to_sign, $access_key_1));
$headers = array("Authorization: SharedKey snip:" . $signature, "x-ms-date: " . $current_date, "x-ms-version: 2009-09-19");
echo send_request($url, $headers);
?>

如果你正在使用PHP,为什么不使用Windows Azure PHP SDK来做到这一点?https://github.com/WindowsAzure/azure-sdk-for-php

已经在真实服务器上进行了很好的测试。