Log in or register to post comments

Unity, C#, VWS API

September 5, 2017 - 2:54am #1

hi i'm trying to export my image to Cloud Database by VWS API. so i refer some forum and make some code. but it doesn't work.

Please Help me.

If i delete "//" in front of the line 'dicHeader.Add("Date", date);', or 'dicHeader.Add("Host", https://vws.vuforia.com);

Error is 'cannot override specific Headers', and current code's error is "result_code":"fail"," traction_id":"*****"

what is problem?? is vuforia sever broken?? i'm looking for problem almost 2weeks. Please help me

public class vwsConnectToCloud : MonoBehaviour
{

public string accessKey = "3de5198ece877f037505************";
public string secretKey = "87ae92f53e764a6aea1a************";

public string url = "https://vws.vuforia.com/targets";
public Texture2D target;
string Date = "";
string contentType = "application/json";

// Use this for initialization
void Start()
{

}

public void calljpostTarget()
{
StartCoroutine(jpostTarget());
}

// Update is called once per frame
void Update()
{

}

IEnumerator jpostTarget()
{
Date = DateTime.UtcNow.ToString("r");
//Date = DateTime.Now.ToUniversalTime().ToString("r");

Dictionary<string, string> test = new Dictionary<string, string>();
test.Add("name", "myTarget");
test.Add("width", "32.0");
test.Add("image", System.Convert.ToBase64String(target.EncodeToPNG()));
string json = JsonWriter.Serialize(test);

HttpWebRequest httpWReq = (HttpWebRequest)HttpWebRequest.Create(url);

//byte[] rawData = req.data;

WWWForm jform = new WWWForm();

jform.AddBinaryData("json", Encoding.UTF8.GetBytes(json));

Dictionary<string, string> dicHeader = new Dictionary<string, string>();

dicHeader = jform.headers;

//dicHeader.Add("Host", "vws.vuforia.com");
//dicHeader.Add("Date", Date);
//dicHeader.Add("Content-Type", "application/json");
dicHeader["Content-Type"] = contentType;
dicHeader.Add("Authorization", "VWS " + accessKey + ":" + signature(secretKey, json));

List<string> test2 = new List<string>(dicHeader.Values);
List<string> test3 = new List<string>(dicHeader.Keys);
for (int i = 0; i < test2.Count; i++)
{
Debug.Log("Test Data " + test3[i] + test2[i]);
}

WWW request = new WWW(url, jform.data, dicHeader);

yield return request;
print(request.responseHeaders["Date"]);
print(request.text);
//yield return www.Send();
}

string signature(string secret, string json)
{
MD5 md5 = MD5.Create();
var contentMD5bytes = md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(json));
StringBuilder sb = new 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}\n{4}", "POST", contentMD5, "application/json", Date, "/targets");

HMACSHA1 sha1 = new HMACSHA1(System.Text.Encoding.ASCII.GetBytes(secret), true);
//HMACSHA1 sha1 = new HMACSHA1(System.Text.Encoding.ASCII.GetBytes(secret));
byte[] sha1Bytes = Encoding.ASCII.GetBytes(StringToSign);
MemoryStream stream = new MemoryStream(sha1Bytes);
byte[] sha1Hash = sha1.ComputeHash(stream);
string signature = System.Convert.ToBase64String(sha1Hash);
return signature;
}
}

Unity, C#, VWS API

February 5, 2018 - 12:37pm #3

You should be able to fix that by making Date all lowercase.. Date -> date. you should do the same for "Host" and "Content-Type"

Unity, C#, VWS API

November 16, 2017 - 12:46pm #2

Hello, did you find the problem?

Log in or register to post comments