【问题标题】:Getting signature for google api获取 google api 的签名
【发布时间】:2019-02-03 13:36:45
【问题描述】:

我正在尝试使用 Google Streetview Static API 来获取大量街景图像。我有一个有效的 API 密钥和 URL 签名密钥,但我在对签名进行编码时遇到了问题。无论我尝试了什么,我都会得到错误的签名,并且 url 不起作用。任何帮助将不胜感激。

这是我所做的(Encode方法不是我的):

    static void Main(string[] args)
    {
        Process.Start(GenerateURL(0, 0, "40.7488115", "-73.9855688", 1920, 1080, 90));

        Console.ReadLine();
    }

    public static string GenerateURL(double heading, double pitch, string locationLat, string locationLong, int resX, int resY, int fov)
    {
        string universalURL = "size=" + resX + "x" + resY + "&location=" + locationLat + "," + locationLong + "&heading=" + heading + "&pitch=" + pitch + "&fov=" + fov + "&key=" + apiKey;
        string getURL = "https://maps.googleapis.com/maps/api/streetview?" + universalURL;
        string getSignature = "_maps_api_streetview?" + universalURL;
        return getURL + "&signature=" + Encode(getSignature, signingKey);
    }

    public static string Encode(string input, string inputkey)
    {
        byte[] key = Encoding.ASCII.GetBytes(inputkey);
        byte[] byteArray = Encoding.ASCII.GetBytes(input);
        using (var myhmacsha1 = new HMACSHA1(key))
        {
            var hashArray = myhmacsha1.ComputeHash(byteArray);
            return hashArray.Aggregate("", (s, e) => s + String.Format("{0:x2}", e), s => s);
        }
    }

我使用 _ 而不是 / 作为 getSignature 的原因是因为 here 它说它需要被替换。我已经尝试过使用 / 但它不起作用。

感谢您的帮助。

【问题讨论】:

    标签: c# google-api google-street-view


    【解决方案1】:

    编辑: 我在谷歌网站上找到了解决方案:

    static void Main(string[] args)
        {
            Process.Start(GenerateURL(0, 0, "-26.235859", "28.077619", 500, 500, 90));
    
            Console.ReadLine();
        }
    
        public static string GenerateURL(double heading, double pitch, string locationLat, string locationLong, int resX, int resY, int fov)
        {
            return Sign("https://maps.googleapis.com/maps/api/streetview?size=" + resX + "x" + resY + "&location=" + locationLat + "," + locationLong + "&heading=" + heading + "&pitch=" + pitch + "&fov=" + fov + "&key=" + apiKey, signingKey);
        }
    
        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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      相关资源
      最近更新 更多