获取 google api 的签名
Getting signature for google api
我正在尝试使用 Google 静态街景 API 获取大量街景图像。我有一个可用的 API 密钥和 URL 签名密钥,但我在对签名进行编码时遇到了问题。无论我尝试过什么,我都会得到错误的签名并且 url 不起作用。任何帮助将不胜感激。
这是我所做的(编码方法不是我的):
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 它说它需要被替换。我已经尝试过 / 但它不起作用。
感谢您的帮助。
编辑:
我在 google 网站上找到了解决方案:
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;
}
我正在尝试使用 Google 静态街景 API 获取大量街景图像。我有一个可用的 API 密钥和 URL 签名密钥,但我在对签名进行编码时遇到了问题。无论我尝试过什么,我都会得到错误的签名并且 url 不起作用。任何帮助将不胜感激。
这是我所做的(编码方法不是我的):
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 它说它需要被替换。我已经尝试过 / 但它不起作用。
感谢您的帮助。
编辑: 我在 google 网站上找到了解决方案:
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;
}