【发布时间】:2017-04-08 20:14:24
【问题描述】:
我正在尝试在 winform 应用程序中访问 Moz API。我查看了 PHP 指南并尝试将其转换为 C#,但遇到了问题。
例如 PHP:https://github.com/seomoz/SEOmozAPISamples/blob/master/php/signed_authentication_sample.php
我认为是我的签名搞砸了。在文档中它说:
“签名:您的访问 ID、Expires 参数和您的密钥的 HMAC-SHA1 哈希。安全哈希必须经过 base64 编码,然后在 Mozscape 接受签名为有效之前进行 URL 编码。”
long epochTicks = new DateTime(1970, 1, 1).Ticks;
long unixTime = ((DateTime.UtcNow.Ticks - epochTicks) / TimeSpan.TicksPerSecond) + 500;
var id = "mozscape-xxxxxxxx";
var secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var enc = Encoding.ASCII;
HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(secretKey));
hmac.Initialize();
var access = "mozscape-fa667096b" + "\r\n" + unixTime;
byte[] buffer = enc.GetBytes(access);
string signature = BitConverter.ToString(hmac.ComputeHash(buffer));
string signatureStep2 = System.Text.Encoding.UTF8.EncodeBase64(signature);
string signatureStep3 = HttpUtility.UrlEncode(signatureStep2);
有什么想法吗?我得到的错误是我的身份验证失败。
【问题讨论】: