Hi,
I am trying to achieve Cloud Recognition through Image Recognition Query of VWS, using unity and C#. I am using client keys instead of server keys as mentioned in different discussions in the forums. But irrespective of the keys being used, the request throws "401: unauthorized" error. I am re-using the code which works fine for uploading a target to cloud database. The code used is as below. Please help in resolving the issue.
private string url = "https://cloudreco.vuforia.com/v1/query";
private string targetName = "MyTarget" + System.DateTime.Now.ToString(); // must change when upload another Image Target, avoid same as exist Image on cloud
void Start()
{
StartCoroutine(PostNewTarget());
}
[System.Serializable]
public class PostNewTrackableRequest
{
public string name;
public string image;
}
IEnumerator PostNewTarget()
{
string serviceURI = url ;
string httpAction = "POST";
string contentType = "multipart/form-data";
string date = string.Format("{0:r}", System.DateTime.Now.ToUniversalTime());
Debug.Log(date);
// if your texture2d has RGb24 type, don't need to redraw new texture2d
Texture2D tex = new Texture2D(texture.width, texture.height, TextureFormat.RGB24, false);
tex.SetPixels(tex.GetPixels());
tex.Apply();
byte[] image = tex.EncodeToPNG();
System.IO.File.AppendAllText(Application.persistentDataPath+"/Debug.txt","byte array of image : "+image+"\n");
//PostNewTrackableRequest model = new PostNewTrackableRequest();
//model.image = System.Convert.ToBase64String(image);
//model.name = targetName;
//model.width = 64.0f; // don't need same as width of texture
//model.image = System.Convert.ToBase64String(image);
string requestBody = JsonWriter.Serialize(System.Convert.ToBase64String(image));
WWWForm form = new WWWForm();
var headers = form.headers;
byte[] rawData = form.data;
headers["Host"] = url;
headers["Date"] = date;
headers["Content-Type"] = contentType;
headers["Accept"] = "application/json";
HttpWebRequest httpWReq = (HttpWebRequest)HttpWebRequest.Create(serviceURI);
MD5 md5 = MD5.Create();
var contentMD5bytes = md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(requestBody));
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < contentMD5bytes.Length; i++)
{
sb.Append(contentMD5bytes[i].ToString("x2"));
}
string contentMD5 = sb.ToString();
string stringToSign = string.Format("{0}\n{1}\n{2}\n{3}", httpAction, contentMD5, contentType, date);
HMACSHA1 sha1 = new HMACSHA1(System.Text.Encoding.ASCII.GetBytes(secret_key));
byte[] sha1Bytes = System.Text.Encoding.ASCII.GetBytes(stringToSign);
MemoryStream stream = new MemoryStream(sha1Bytes);
byte[] sha1Hash = sha1.ComputeHash(stream);
string signature = System.Convert.ToBase64String(sha1Hash);
headers["Authorization"] = string.Format("VWS {0}:{1}", access_key, signature);
WWW request = new WWW(serviceURI, System.Text.Encoding.UTF8.GetBytes(JsonWriter.Serialize(System.Convert.ToBase64String(image))), headers);
yield return request;
if (request.error != null)
{
status.text = request.error;
Debug.Log("request error: " + request.error);
}
else
{
status.text = "Success";
Debug.Log("request success");
Debug.Log("returned data" + request.text);
}
}
}
Hi,
Thank you for the feedback.
We updated the articles https://library.vuforia.com/articles/Solution/How-To-Perform-an-Image-Recognition-Query and https://library.vuforia.com/articles/Training/Using-the-VWS-API.html to correct the inconsistencies.
Br,
Vuforia Engine Support