11

I am trying to implement vuforia api in C# code.

I am getting an error from the server.

C# Code:

ASCIIEncoding Encoding = new ASCIIEncoding();
MD5 md5 = MD5.Create();
string requestPath = "/targets";
string serviceURI = "https://vws.vuforia.com"+ requestPath;
string httpVerb = "GET";
var contentMD5bytes = md5.ComputeHash(Encoding.GetBytes(""));
string contentMD5 = BitConverter.ToString(contentMD5bytes).Replace("-", "");
string contentType = "";
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
string date = string.Format("{0:r}", DateTime.Now.ToUniversalTime());
string StringToSign = String.Format("{0}\n{1}\n{2}\n{3}\n{4}", httpVerb, contentMD5, contentType, date, requestPath); // HTTP-Verb, Content-MD5, Content-Type, Date, Request-Path;
HMACSHA1 sha1 = new HMACSHA1(System.Text.Encoding.ASCII.GetBytes(secretKey));
byte[] sha1Bytes = Encoding.GetBytes(StringToSign);
MemoryStream stream = new MemoryStream(sha1Bytes);
byte[] sha1Hash = sha1.ComputeHash(stream);
string signature = System.Convert.ToBase64String(sha1Hash);

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(serviceURI);
request.Headers.Add("Authorization", string.Format("VWS {0}:{1}", accessKey, signature));
request.Date = DateTime.Now.ToUniversalTime();

try
{
    using (HttpWebResponse responses = request.GetResponse() as HttpWebResponse)
    {
        if (responses.StatusCode == HttpStatusCode.OK)
        {
            // Do stuff with response.GetResponseStream();
        }
    }
}
catch (WebException ex)
{

}

I am getting the following error when reaches

WebResponse tResponse = tRequest.GetResponse();

Error: {"The remote server returned an error: (401) Unauthorized."}

I referred the following urls

https://developer.vuforia.com/forum/qcar-api/vms-api-using-c

Can anyone provide the solution for this.

Dean Seo
  • 5,486
  • 3
  • 30
  • 49
chozha rajan
  • 355
  • 4
  • 14
  • Do you have any documentation on the VWS authorization type? It's hard to get anywhere with just code from a forum that also contains an error. – C.Evenhuis Jan 11 '18 at 08:00
  • I get The authentication scheme from this url https://library.vuforia.com/articles/Solution/Using-the-VuMark-Instance-Generation-API – chozha rajan Jan 11 '18 at 08:19
  • Likely to cause problems only when you're stepping through the code in the debugger: they say the date in the authorization must **exactly** match the date header in the request, so you should keep the result of `DateTime.Now` in a variable and use that for both. Else they might be off by a few seconds. – C.Evenhuis Jan 11 '18 at 09:31
  • I also had the same doubt and i tried by keeping the value in a separate variable.But still getting the same error – chozha rajan Jan 11 '18 at 09:36
  • "String.Format("{0}\n{1}\n{2}\n{3}\n{4}", httpVerb, contentMD5, contentType, date, requestPath); " -- here i am passing date value as "Thu, 11 Jan 2018 09:38:26 GMT" and "request.Date"--here it is "11/01/2018 03:08:26 PM".I tried to pass "Thu, 11 Jan 2018 09:38:26 GMT" to request.Date.But I can't able to assign string value(GMT format) to an DateTime Variable – chozha rajan Jan 11 '18 at 09:42
  • The `request.Date` property will be formatted as GMT when you actually send your request. – C.Evenhuis Jan 11 '18 at 13:06
  • Ok, then what is the problem in my code.Why I am getting this error? – chozha rajan Jan 11 '18 at 13:17
  • i dont think there is problem with your code try clearing your cookies, cache log-in and log-out. – NoobProgrammer Jan 24 '18 at 06:14
  • I believe a space is missing between the 'Authorization' and the 'VWS' words in the Authorization header – Jorge Cavaleiro Dias Jan 13 '19 at 10:12

0 Answers0