我用C#语言编写。我在c#上重写了Java示例中的代码,但是,当我尝试发送请求并获得响应时,我收到身份验证失败(401)(签名身份验证失败)的信息。自动化本身看起来像: 授权:VWS {provision_access_key}:{签名} 获得签名的位置:
Signature = Base64(HMAC-SHA1(server_secret_key, StringToSign ) ) ;
StringToSign =
HTTP-Verb + "\n" +
Content-MD5 + "\n" +
Content-Type + "\n" +
Date + "\n" +
Request-Path;
在代码中,我具有函数setHeaders
,这是用于请求的函数集标头,她看起来是这样的:
public void setHeaders(HttpWebRequest request)
{
string secret_key = "da6048fa067245b751a1d7ca5dca61b4ca751ac7";
string access_key = "e34c6fe8a8ad99c48e436791c863a738df2b9113";
SignatureBuilder sc = new SignatureBuilder();
request.Date = DateTime.UtcNow.ToUniversalTime();
request.Headers.Add("Autorization", "VWS" + access_key + ":" + sc.tmsSignature(request, secret_key));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = request.GetRequestStream();
stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
Console.WriteLine(reader.ReadToEnd());
}
我有SignatureBuilder类,其中定义了函数tmsSignature,ContentMD5,CalculateRFC2104HMAC
。
class SignatureBuilder
{
public string tmsSignature(HttpWebRequest request, string seckretKey)
{
WebClient webClient = new WebClient();
string method = request.Method;
request.Date = Convert.ToDateTime(DateTime.UtcNow.ToString("R"));
string ContentType = "";
string HexDigest = "d41d8cd98f00b204e9800998ecf8427e";//for request types without request body include the MD5 hash of an empty string which is “d41d8cd98f00b204e9800998ecf8427e”.
if (method.Equals("GET", StringComparison.OrdinalIgnoreCase) || method.Equals("DELETE", StringComparison.OrdinalIgnoreCase))
{ }
else if (method.Equals("POST", StringComparison.OrdinalIgnoreCase) || method.Equals("PUT", StringComparison.OrdinalIgnoreCase))
{
ContentType = "application/json";
HexDigest = contentMD5(request);
}
else
{ System.Console.WriteLine("Error: invalid content type passed to Sig Builder"); }
string dateValue = request.Date.ToString("R");
Console.WriteLine(dateValue);
string requestPath = request.RequestUri.ToString();
string toDigest = method + '\n' + HexDigest + '\n' + ContentType + '\n' + dateValue + '\n' + requestPath;
Console.WriteLine(toDigest);
string shaHashed = "";
try
{
shaHashed = CalculateRFC2104HMAC(seckretKey, toDigest);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return shaHashed;
}
private string contentMD5(HttpWebRequest request)//Compute hash md5
{
byte[] buffer = new byte[16];
using (Stream stream = request.GetRequestStream())
{
stream.Write(buffer, 0, 16);
}
byte[] hash = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(buffer));
MD5 md5 = new MD5CryptoServiceProvider();
byte[] hashenc = md5.ComputeHash(hash);
string result = "";
foreach (var b in hashenc)
{
result += b.ToString("x2");
}
Console.WriteLine(result);
return result;
}
public static string CalculateRFC2104HMAC(string key, string data)//Compute hash base 64 with HMACSHA1
{
UTF8Encoding utf8 = new UTF8Encoding();
string result = "";
var sha = KeyedHashAlgorithm.Create("HMACSHA1");
sha.Key = Encoding.UTF8.GetBytes(key);
result = Convert.ToBase64String(sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(data)));
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(result);
Console.ResetColor();
return result;
}
}
但是,如果我尝试发送get或post请求,我总是会收到错误身份验证失败。也许每个人都会发现隐藏错误在哪里?
static void Main(string[] args)
{
HttpWebRequest qwe = (HttpWebRequest)WebRequest.Create("https://vws.vuforia.com/targets");
qwe.Method = "GET";
PostNewTarget postNewTarget = new PostNewTarget();
postNewTarget.setHeaders(qwe);
}