我正在尝试使用 PHP 和 Laravel 将图像目标添加到 Vuforia Web API。我遵循了 Vuforia Web API 文档并按照描述实施了身份验证过程。但是,当我发出添加图像目标的 API 请求时,我收到以下错误:
错误:{"result_code":"Fail","transaction_id":"5e5f2b5e8e624c2d8e5f2b5e8e622f6d","result_string":"Bad Request - Invalid Content-MD5 header"}
我仔细检查了我的实现,我相信我正在根据原始请求正文正确计算 Content-MD5 哈希。这是我生成请求标头和签署请求的代码片段:
public function addTarget(Request $request)
{
$request->validate([
'name' => 'required|string',
'width' => 'required|numeric',
'image' => [
'required',
'file',
'mimes:jpg,jpeg,png',
'max:2048',
'dimensions:min_width=320',
],
'animation_name' => 'required|string',
'audio_name' => 'required|string',
'video_name' => 'required|string',
]);
$client = new Client();
$imageFile = $request->file('image')->getPathname();
$imageEncoded = base64_encode(file_get_contents($imageFile));
// Create metadata JSON object
$metadata = [
'animation_name' => $request->input('animation_name'),
'audio_name' => $request->input('audio_name'),
'video_name' => $request->input('video_name'),
];
$metadataJson = json_encode($metadata);
$metadataEncoded = base64_encode($metadataJson);
$requestBody = [
'name' => $request->input('name'),
'width' => $request->input('width'),
'image' => $imageEncoded,
'application_metadata' => $metadataEncoded
];
$requestBodyRaw = json_encode($requestBody);
$url = 'https://vws.vuforia.com/targets';
$headers = $this->getHeaders($requestBody);
\Log::debug('Request body: ' . json_encode($requestBody));
\Log::debug('Headers: ' . json_encode($headers));
\Log::debug('String to sign: ' . $this->getStringToSign($requestBody));
try {
$response = $client->post($url, [
'body' => $requestBodyRaw,
'headers' => $headers,
'verify' => config('services.curl_cainfo'),
]);
$result = json_decode($response->getBody(), true);
// Store the image in the local storage
$imageName = $request->file('image')->getClientOriginalName();
$request->file('image')->storeAs('public/images', $imageName);
$imagePath = 'storage/images/' . $imageName;
// Save the image and metadata in the PostgreSQL database
$imageTarget = new imageTarget();
$imageTarget->name = $request->input('name');
$imageTarget->width = $request->input('width');
$imageTarget->image_path = $imagePath;
$imageTarget->metadata = $metadataJson;
$imageTarget->save();
return view('success', ['result' => $result]);
} catch (\Exception $e) {
return view('error', ['message' => $e->getMessage()]);
}
}
private function getHeaders($requestBody)
{
$serverAccessKey = '7970dca2f5f6d6e8c577a41eaf17aff759f1d94b';
$serverSecretKey = '6c30fd48028cd24caebc52d0761b4b121bcc6cc0';
$date = gmdate('D, d M Y H:i:s') . ' GMT';
$method = 'POST';
$path = '/targets';
$requestBodyRaw = json_encode($requestBody);
$contentMD5 = base64_encode(md5($requestBodyRaw, true));
$contentType = 'application/json';
$stringToSign = "{$method}\n{$contentMD5}\n{$contentType}\n{$date}\n{$path}";
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $serverSecretKey, true));
return [
'Host' => 'vws.vuforia.com',
'Date' => $date,
'Content-Type' => $contentType,
'Authorization' => "VWS {$serverAccessKey}:{$signature}"
];
}
private function getStringToSign($requestBody)
{
$date = gmdate('D, d M Y H:i:s') . ' GMT';
$method = 'POST';
$path = '/targets';
$requestBodyRaw = json_encode($requestBody);
$contentMD5 = base64_encode(md5($requestBodyRaw, true));
$contentType = 'application/json';
return "{$method}\n{$contentMD5}\n{$contentType}\n{$date}\n{$path}";
}```
I have tried several adjustments to my code, but I still receive the same error. I would appreciate any guidance or suggestions from anyone who has encountered a similar issue or knows how to resolve it.
Thank you.
I followed the Vuforia Web API documentation to implement adding an image target using PHP and Laravel. I expected to successfully add an image target and receive a response from the API with a result_code of "Success" and a transaction_id.
Here are the steps I took:
I created a new Laravel project and set up a route to handle the image target submission.
I implemented the authentication process as described in the Vuforia Web API documentation, using the server access key and server secret key.
I created a function to handle the request, validate the input, and generate the request body, including the image and metadata.
I implemented the calculation of the Content-MD5 hash based on the raw request body, as well as the creation of the signature for the Authorization header.
I sent the API request using Guzzle HTTP Client with the generated headers and request body.
However, I received the following error:
Error: {"result_code":"Fail","transaction_id":"5e5f2b5e8e624c2d8e5f2b5e8e622f6d","result_string":"Bad Request - Invalid Content-MD5 header"}
Despite multiple attempts to troubleshoot and adjust my implementation, I continue to encounter this error. I have reviewed the relevant sections of the Vuforia Web API documentation and made sure to follow the steps provided.
I would appreciate any suggestions or insights on how to resolve this issue and successfully add an image target using the Vuforia Web API.