【问题标题】:Using Moz API in C#在 C# 中使用 Moz API
【发布时间】: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);

有什么想法吗?我得到的错误是我的身份验证失败。

【问题讨论】:

    标签: c# .net api hash


    【解决方案1】:

    通过查看https://github.com/PixelMEDIA/SEOMozLib 解决了这个问题。

    这是正确生成密钥的方法:

    public string CreateHashSignature(string strMozAccessId, string strMozSecretKey, string strTimeStamp)
        {
            string token = strMozAccessId + Environment.NewLine.Replace("\r", "") + strTimeStamp;
    
            using (var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(strMozSecretKey), true))
            {
                var hash = hmac.ComputeHash(Encoding.ASCII.GetBytes(token));
                var hashString = BitConverter.ToString(hash).Replace("-", "").ToLower();
                return HttpUtility.UrlEncode(Convert.ToBase64String(hash));
            }
        }
    

    【讨论】:

      【解决方案2】:

      你可以使用我的库 LINQTOMOZ https://github.com/PavelMatua/LinqToMoz

      只需将您的访问密钥和安全密钥放入 MOZService 构造函数即可。我们开始吧!

      MOZService service = new MOZService("your access key","your security key");
      var urlMetrics = service.QueryURLMetrics();
      var result = urlMetrics.Where((arg) => sites.Contains(arg.SearchingURL) && arg.SourceCols == URLMetricsCols.FREE)
          .Select(x => new { equityLinkNumber = x.MetricsResult.ueid, cononicalURL = x.MetricsResult.uu })
          .OrderByDescending(x => x.equityLinkNumber);
      

      【讨论】:

      • 这个问题已经有一个被接受的答案。您的答案与已经接受的答案相比有何改进?
      • 1 行代码而不是 10 行......在你需要查询数据之后......所以我发现它对人们很有用:)
      猜你喜欢
      • 1970-01-01
      • 2022-10-18
      • 2010-12-09
      • 1970-01-01
      • 2012-12-10
      • 2013-09-08
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      相关资源
      最近更新 更多