我有一个大问题。我正在使用C#开发UWP Windows 10应用程序,希望使用OAuth 1。
一切几乎都没问题,但签名错误。不过,在Microsoft GitHub上找到了示例代码。显然,我做了一些修改...
我的代码:
private async Task GoCo()
{
String LifeInvaderUrl = "http://stage.api.lolilolz.be/v8/login";
string timeStamp = GetTimeStamp();
string nonce = GetNonce();
string consumerKey = "noob-stage";
string consumerSecret = "TOPSECRETxxXXxx";
string SigBaseStringParams = "oauth_consumer_key=" + consumerKey;
SigBaseStringParams += "&" + "oauth_signature_method=HMAC-SHA1";
SigBaseStringParams += "&" + "oauth_timestamp=" + timeStamp;
SigBaseStringParams += "&" + "oauth_nonce=" + nonce;
SigBaseStringParams += "&" + "oauth_version=1.0";
string SigBaseString = "POST&";
SigBaseString += Uri.EscapeDataString(LifeInvaderUrl) + "&" + Uri.EscapeDataString(SigBaseStringParams);
String Signature = GetSignature(SigBaseString, consumerSecret);
string authorizationHeaderParams = "oauth_consumer_key=\"" + consumerKey + "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + timeStamp + "\", oauth_nonce=\"" + nonce + "\", oauth_vesrion=\"1.0\", oauth_signature=\"" + Uri.EscapeDataString(Signature)+ "\"";
HttpClient httpClient = new HttpClient();
//...
}
并且签名生成器方法:
string GetSignature(string sigBaseString, string consumerSecretKey)
{
IBuffer KeyMaterial = CryptographicBuffer.ConvertStringToBinary(consumerSecretKey + "&", BinaryStringEncoding.Utf8);
MacAlgorithmProvider HmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
CryptographicKey MacKey = HmacSha1Provider.CreateKey(KeyMaterial);
IBuffer DataToBeSigned = CryptographicBuffer.ConvertStringToBinary(sigBaseString, BinaryStringEncoding.Utf8);
IBuffer SignatureBuffer = CryptographicEngine.Sign(MacKey, DataToBeSigned);
string Signature = CryptographicBuffer.EncodeToBase64String(SignatureBuffer);
return Signature;
}
提前感谢你 :)