Log in or register to post comments

Help me to use VWS API with php

April 6, 2013 - 4:43am #1

My problem:

- I have a web application, my client side can open an image and modify it in some ways, generate metadata, etc... and then upload the image and those data to my server.

- My server side after receive all those things will use the secret and access key from Target Manager and the VWS api to upload them to Cloud Database.

- I follow this guide to set up the authorize header and this guide to post new image to database, but I always got 401 error, I think I had some problems with encoding the hmac sha1... 

This is my php code at server side, I checked the upload part, it upload to my server ok.

<?php

$path = "data/uploads/";
$fail = true;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
	if($_FILES['imagetarget']['name'])
	{
		$tmp = $_FILES['imagetarget']['tmp_name'];
		$name = basename($_FILES['imagetarget']['name']);
		if(move_uploaded_file($tmp, $path.$name))
		{

			// http request body
			$now = new DateTime('NOW');
			$body = json_encode(array(
				"name" => $name,
				"width" => 1024.0,
				"image_url" => base64_encode($path.$name),
				"active_flag" => 1,
				"application_metadata_url" => base64_encode($_POST["metadata"]))
				);

			$http_verb = "POST";
			$content_md5 = md5($body);
			$content_type = "application/json";
			$date = str_replace("+0000", "GMT", $now->format(DateTime::RFC1123));
			$request_path = "https://vws.vuforia.com/targets";

			// auth string for header
			$string_to_sign = $http_verb . "\n" . $content_md5 . "\n" . $content_type . "\n" . $date . "\n" . $request_path;
			$secret_key = "my_secret_key";
			$signature = hash_hmac("sha1", $string_to_sign, $secret_key);
			$authstring = "AWS " . $secret_key . ":" . $signature;

			// the request
			$request = new HttpRequest($request_path, HttpRequest::METH_POST);
			$request->setContentType($content_type);
			$request->setBody($body);
			$request->addHeaders(array(
				"Date" => $date,
				"Authorization" => $authstring));

			$request->send();

			echo $request->getRequestMessage();
			echo $request->getResponseMessage();
		}
	}
}

?>

 

This is my raw request message and response message:

POST /targets HTTP/1.1 
User-Agent: PECL::HTTP/1.7.5 (PHP/5.4.10) 
Host: vws.vuforia.com Accept: */* 
Date: Sat, 06 Apr 2013 11:26:43 GMT 
Authorization: AWS 189d29ef1419ce28fe919d9234d3b56eeb240e64:665bbdb813970cf8af8ec2a85230a0915506530b 
Content-Type: application/json 
Content-Length: 139 
{"name":"Image(266).jpg","width":1024,"image_url":"ZGF0YS91cGxvYWRzL0ltYWdlKDI2NikuanBn","active_flag":1,"application_metadata_url":"W10="} 


HTTP/1.1 401 Unauthorized 
Server: Apache-Coyote/1.1 
Content-Type: application/json;charset=UTF-8 
Date: Sat, 06 Apr 2013 11:27:03 GMT 
X-Original-Transfer-Encoding: chunked 
Content-Length: 91 {"result_code":"AuthenticationFailure","transaction_id":"15cbee66007a4df28c982d0a00730642"}

 

Any help is great appreciated.

Help me to use VWS API with php

September 12, 2017 - 2:17pm #6

I cant't well you what you're doing wrong, but there is a basic PHP class to connect and handle Vuforia Cloud Recognition.

Maybe this helps: https://github.com/FionNoir/VuforiaClient

The answer to your second question is, that metadata can be any kind of additional information that you want to save. So you can put anything you want into the metadata string.

Hope this helps!

Help me to use VWS API with php

July 23, 2016 - 6:18am #5

0
down vote
favorite
I want t upload target images from my web page to vuforia cloud recognition database through VWS Api but I got error messages. I added server access key(secret key) and followed all steps given on vuforia page too.

please somebody help me.`
This is my code part:

<?php
$path = "uploads/";
$fail = true;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
    if($_FILES['file']['name'])
    {
        $tmp = $_FILES['file']['tmp_name'];
        $name = basename($_FILES['file']['name']);
        if(move_uploaded_file($tmp, $path.$name))
        {

            // http request body
            $now = new DateTime('NOW');
            $body = json_encode(array(
                "image_url" => base64_encode($path.$name),
                "active_flag" => 1,
                "application_metadata_url" => base64_encode($_POST["metadata"])
               )
                );

            $http_verb = "POST";
            $content_md5 = md5($body);
            $content_type = "application/json";
            $date = str_replace("+0000", "GMT", $now->format(DateTime::RFC1123));
            $request_path = "<a href="https://vws.vuforia.com/targets";">https://vws.vuforia.com/targets";</a>

            // auth string for header
            $string_to_sign = $http_verb . "\n" . $content_md5 . "\n" . $content_type . "\n" . $date . "\n" . $request_path;
            $secret_key = "my secret key";
            $signature = hash_hmac("sha1", $string_to_sign, $secret_key);
            $authstring = "VWS " . $secret_key . ":" . $signature;

            // the request
            $request = new HttpRequest($request_path, HttpRequest::METH_POST);
            $request->setContentType($content_type);
            $request->setBody($body);
            $request->addHeaders(array(
                "Date" => $date,
                "Authorization" => $authstring));

            $request->send();

            echo $request->getRequestMessage();
            echo $request->getResponseMessage();
        }
    }
}

?>

 

Q1:please some body tell me what am I doing wrong?
Q2: secondly I copied this line "application_metadata_url" => base64_encode($_POST["metadata"] from some website. what does that metadata mean? and how I can get that for my image that I am uploading on the web page?

Please help me or show me the complete code. It would be highly appreciated. 

help me to Retrieving a Target from a Cloud Database

June 23, 2014 - 1:16am #4

Hello, 

 I was following the tutorial about "Retrieving Target Database from a Cloud" but I get stuck running when executing the method "setRequestBody", and when excuting the method "setHeaders". What could cause this problem?

Help me to use VWS API with php

May 5, 2013 - 10:49am #3

I figured out the problem long time ago.

I just mistake "VWS" with "AWS".. yeah, I worked with Amazon Cloud alot.. What a stupid mistake ^^!

 

Anyway, thank you for your help and sorry for late reply.

Help me to use VWS API with php

April 7, 2013 - 10:47am #2

The problem may be due to your date format. Try formatting it this way ..

$date = new DateTime("now", new DateTimeZone("GMT"));
$datetime = $date->format("D, d M Y H:i:s ") . " GMT";

That will add the necessary comma after the Day and append the date w/ GMT.

Also see the code for tranforming the SHA1 hash to base 64 in this thread..

https://developer.vuforia.com/forum/general-discussion/vws-authorization-problem

It may be a good idea to first verify that you generating the Authorization header properly, using the empty MD5 string and using a GET query - and then add the body and POST operation once this is confirmed.

 

Log in or register to post comments