Log in or register to post comments

Unable to submit binary image payload to Cloud Recognition Endpoint

December 13, 2016 - 7:35am #1

Hello,

 I'm currently trying to test the Cloud Recognition API and have been able to successfully send requests getting non-authentication errors.  This leads me to believe my signature builder code is accurate.  I am, however unable to get anything other than Unauthorized messages when sending a binary payload image.

I have believe I have verified that I am using the correct client vs server keys, as I stated previously I was able to get non-unauthorized messages from the server.

I receive the following response {"transaction_id":"80d2a2998fa24467b9e761a6b46eb634","result_code":"AuthenticationFailure"}

Below is my source code and attached is the sample image.  Any assistance would be wonderful and expedience would be most appreciated.

Thank you!

 

public class CloudRecognition
	{

		const string _accessKey = "<redacted>";
		const string _secretKey = "<redacted>";
		const string _cloudServicesUrl = "https://cloudreco.vuforia.com";
		const string _contentType = "multipart/form-data;";
		const string _imageName = "Y1159546.jpg";

		string _hexDigest = "d41d8cd98f00b204e9800998ecf8427e"; // Hex digest of an empty string

		public string RequestMatch()
		{
			RestClient client = new RestClient(_cloudServicesUrl);
			RestRequest request = new RestRequest("v1/query");

			string imagePath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Documents"), _imageName);
			byte[] image = File.ReadAllBytes(imagePath);

			request.AlwaysMultipartFormData = true;
			request.AddFile("image", image, _imageName, "image/jpeg");

			using (MD5 md5Hash = MD5.Create())
			{
				_hexDigest = CreateMD5HashString(md5Hash, image);
			}

			request.Method = Method.POST;
			request.AddHeader("Date", GetCurrentUTCTimeString());
			request.AddHeader("Content-MD5", _hexDigest);
			request.AddHeader("Content-Type", _contentType);

			request.AddHeader("Authorization", BuildAuthorizationHeader(request));

			var restResponse = client.Execute(request);

			return restResponse.Content;
		}

		string BuildAuthorizationHeader(RestRequest request)
		{
			StringBuilder headerValue = new StringBuilder($"VWS {_accessKey}:");

			string signature = new SignatureBuilder().GetSignature(request, _secretKey, _hexDigest);

			headerValue.Append(signature);

			return headerValue.ToString();
		}

		string GetCurrentUTCTimeString()
		{
			return DateTime.UtcNow.ToString("R");
		}

		public string CreateMD5HashString(MD5 md5Hash, byte[] data)
		{
			byte[] hash = md5Hash.ComputeHash(data);
			StringBuilder sb = new StringBuilder();

			for (int i = 0; i < hash.Length; i++)
			{
				sb.Append(hash[i].ToString("X2"));
			}

			return sb.ToString().ToLowerInvariant();
		}
}

public class SignatureBuilder
	{
		public string GetSignature(RestRequest request, string secretKey, string hexDigest)
		{
			var method = Enum.GetName(typeof(Method), request.Method);
			var dateValue = ExtractDateFromParameters(request.Parameters);
			var contentType = ExtractContentTypeFromParameters(request.Parameters);

			string digestString = $"{method}\n{hexDigest}\n{contentType}\n{dateValue}\n/{request.Resource}";

			byte[] secretKeyBytes = Encoding.UTF8.GetBytes(secretKey);

			return CreateRFC2104HMAC(digestString, secretKeyBytes);
		}

		string CreateRFC2104HMAC(string input, byte[] key)
		{
			byte[] byteArray = Encoding.UTF8.GetBytes(input);
			using (HMACSHA1 myhmacsha1 = new HMACSHA1(key))
			{
				MemoryStream stream = new MemoryStream(byteArray);
				byte[] hmacBytes = myhmacsha1.ComputeHash(stream);

				return Convert.ToBase64String(hmacBytes);
			}
		}

		string ExtractDateFromParameters(List parameters)
		{
			return parameters.First(p => p.Name == "Date").Value.ToString();
		}

		string ExtractContentTypeFromParameters(List parameters)
		{
			return parameters.First(p => p.Name == "Content-Type").Value.ToString();
		}
	}
AttachmentSize
Image icon Y1159546.jpg556.41 KB

Unable to submit binary image payload to Cloud Recognition Endpoint

December 20, 2016 - 2:35pm #4

Hello,

If you submit the exact same data using our Python samples, does this fail as well?

https://developer.vuforia.com/downloads/samples

Thanks.

Unable to submit binary image payload to Cloud Recognition Endpoint

December 14, 2016 - 7:19am #3

Correct, I am using the client keys for the Cloud Recognition query API.

Unable to submit binary image payload to Cloud Recognition Endpoint

December 13, 2016 - 3:00pm #2

Hello,

To confirm, you're using the client keys for making queries to the system? The server keys are for provisioning and managing targets.

Thanks.

Log in or register to post comments