获得谷歌API的签名

问题描述 投票:0回答:1

我正在尝试使用Google Streetview Static API来获取大量的街景图像。我有一个有效的API密钥和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个回答
0
投票

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

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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.