. net Webmaster API不正确地编码url (google . apis .Webmaster .v3)



我一直在努力让新的API工作。在执行查询时,我经常得到这个错误:

解析NaN值错误。路径",行0,位置0.

从调查我认为。net代码是不正确的编码url在请求中留下斜杠(/)未编码。这会改变请求的URL路径并导致404。

如果您从请求的{site}部分省略http://部分,它可以工作。例如 domain.com 不是 http://domain.com/

如果你使用的是https网站,就没有办法了。您也不能发出任何请求,要求您在主页之外传递特定的URL,因为它需要包含斜杠(/)。

这就是我在Google Geocode API中是如何做到的。不知道这是否对你有帮助,但希望你能明白。

public static string Sign(string url, string keyString)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            // converting key to bytes will throw an exception, need to replace '-' and '_' characters first.
            string usablePrivateKey = keyString.Replace("-", "+").Replace("_", "/");
            byte[] privateKeyBytes = Convert.FromBase64String(usablePrivateKey);
            Uri uri = new Uri(url);
            byte[] encodedPathAndQueryBytes = encoding.GetBytes(uri.LocalPath + uri.Query);
            // compute the hash
            HMACSHA1 algorithm = new HMACSHA1(privateKeyBytes);
            byte[] hash = algorithm.ComputeHash(encodedPathAndQueryBytes);
            // convert the bytes to string and make url-safe by replacing '+' and '/' characters
            string signature = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_");
            // Add the signature to the existing URI.
            return uri.Scheme + "://" + uri.Host + uri.LocalPath + uri.Query + "&signature=" + signature;
        }

此问题正在跟踪:

https://github.com/google/google-api-dotnet-client/issues/534

它与使用。net 4.0有关,可以通过升级到。net 4.5来修复

最新更新