我想使用VWS API上传目标图像,但是401 Unauthorized

时间:2015-09-04 07:47:27

标签: c# unity3d vuforia

我想在unity3d C#中使用VWS API(POST请求)上传目标# 但我的代码给我留言401 Unauthorized keep on on ...

你能帮帮我吗?我该如何解决?

public class Test3 : MonoBehaviour
{


    private string requestPath = "/targets";
    private string contentMD5;
    private string httpVerb = "POST";
    private string contentType = "application/json";
    private string JsonData;
    private string StringToSign;
    private string image_json;
    private string dateTimeString = DateTime.UtcNow.ToString("ddd, dd MMM yyy HH:mm:ss") + " GMT";
    private string accessKey = "ec43f37f9b825b446fd1fe8f8b1e1892c9fd378b"; // Assign Access Key in the inspector
    private string secretKey = "1f9748cdd2d017e629fe6ee7e8af8d58bca29440"; // Assign Secret Key in the inspector
    private const string md5Empty = "d41d8cd98f00b204e9800998ecf8427e"; // md5empty
    string Host = "vws.vuforia.com";

    void OnGUI()
    {
        Rect A = new Rect(10, 100, 100, 100);

        if (GUI.Button(A, "ClickHere"))
        {
            StartCoroutine("Start1");
        }
    }


    private string image_encode()
    {

        Texture2D tex = new Texture2D(Screen.width, Screen.height);

        tex.Apply();
        System.IO.File.WriteAllBytes(Application.streamingAssetsPath + "/png.png", tex.EncodeToPNG());
        string image = System.Convert.ToBase64String(tex.EncodeToPNG());

        return image;
    }
    IEnumerator Start1()
    {

        image_json = image_encode();
        JsonData = "{\"name\":test,\"width\":100.0,\"image\":" + image_json + "}";
        Debug.Log("image :" + image_json);
        Debug.Log("StArt Access VWS Server");
        byte[] contentMD5bytes = System.Text.ASCIIEncoding.UTF8.GetBytes(JsonData + md5Empty);
        contentMD5 = GetMd5Hash(contentMD5bytes); //content-md5

        //string StringToSign = String.Format("{0}\n{1}\n{2}\n{3}\n{4}", httpVerb, contentMD5, "d41d8cd98f00b204e9800998ecf8427e", dateTimeString, requestPath);
        StringToSign = httpVerb + "\n" + contentMD5 + "\n" + contentType + "\n" + dateTimeString + "\n" + requestPath;
        //string signature = Encode(secretKey, StringToSign);
        string signature = BuildSignature(secretKey, StringToSign);
        Debug.Log(signature);
        WWWForm form = new WWWForm();
        Hashtable headers = form.headers;

        headers["Host"] = Host;
        headers["Date"] = dateTimeString;
        headers["Authorization"] = "VWS " + accessKey + ":" + signature;
        headers["Content-Type"] = contentType;

        WWW www = new WWW("https://vws.vuforia.com/targets", SetJson(JsonData), headers);

        yield return www;
        Debug.Log(www.error);

    }
    private byte[] SetJson(string json_Data)
    {
        byte[] pData = Encoding.UTF8.GetBytes(json_Data);
        return pData;
    }

    private string BuildSignature(string keyString, string stringToSign)
    {

        byte[] key = System.Text.ASCIIEncoding.UTF8.GetBytes(keyString);
        byte[] data = System.Text.ASCIIEncoding.UTF8.GetBytes(stringToSign);

        HMACSHA1 myhmacsha1 = new HMACSHA1(key);
        MemoryStream stream = new MemoryStream(data);

        //Compute the sha1 hash
        byte[] hash = myhmacsha1.ComputeHash(stream);

        //Convert to Base64 string
        string signature1 = System.Convert.ToBase64String(hash);
        Debug.Log("SIG: " + signature1);
        return signature1;
    }

    static string GetMd5Hash(byte[] toHash)
    {
        byte[] data;
        using (MD5 md5Hash = MD5.Create())
        {
            data = md5Hash.ComputeHash(toHash);
        }
        StringBuilder sBuilder = new StringBuilder();

        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        string hash = sBuilder.ToString();
        Debug.Log("MD5Hash: " + hash);
        return hash;
    }

}

0 个答案:

没有答案