我想在表存储服务中执行嵌入表实体REST API,但它不起作用。
我提到了this doc。
我的请求代码在这里,
<?php
// storage account name
$account = "mystgaccount";
$tablestoragename = "mytablename";
$accessKey = "myaccesskey";
$date = gmdate('D, d M Y H:i:s T',time());
$api_version = "2015-12-11";
$url = "https://$account.table.core.windows.net/$tablestoragename";
$method = "POST";
$body = '{"Address":"MountainView","Age":23,"AmountDue":200.23,"CustomerCode@odata.type":"Edm.Guid","CustomerCode":"c9da6455-213d-42c9-9a793e9149a57833","CustomerSince@odata.type":"Edm.DateTime","CustomerSince":"2008-0710T00:00:00","IsActive":true,"NumberOfOrders@odata.type":"Edm.Int64","NumberOfOrders":"255","PartitionKey":"mypartitionkey","RowKey":"myrowkey"}';
$stringToSign = [
// VERB
$method,
// Content-MD5
'',
// Content-Type
'',
// Date
$date,
];
$stringToSign = array_merge($stringToSign, ["/$account/$tablestoragename"]);
$stringToSign = implode("\n", $stringToSign);
$signature = base64_encode(hash_hmac('sha256', $stringToSign, base64_decode($accessKey), true));
$headers = [
"x-ms-date:{$date}",
"x-ms-version:$api_version",
"Authorization:SharedKey $account:{$signature}",
"Content-Type: application/json",
];
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => http_build_query($body),
);
curl_setopt_array($ch, $proxy_options);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
var_dump($response);
echo curl_error($ch);
curl_close($ch);
?>
响应
"<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code>AuthenticationFailed</m:code><m:message xml:lang="en-US">Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:b93fe560-0002-0028-129a-571361000000
Time:2017-11-07T07:34:13.3303714Z</m:message></m:error>"
我阅读了相关问题,但尚未解决:(
你有什么想法吗?
答案 0 :(得分:0)
此错误的一个可能原因可能是Content-Type
标头。您在请求中指定了此标头,但未包含在$stringToSign
中。请将$stringToSign
更改为:
$stringToSign = [
// VERB
$method,
// Content-MD5
'',
// Content-Type
'application/json',
// Date
$date,
];