c# - 获取 google api 的签名
问题描述
我正在尝试使用 Google Streetview Static 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 的原因是因为这里它说它需要被替换。我已经尝试过 / 但它不起作用。
谢谢你的帮助。
解决方案
编辑:我在谷歌网站上找到了解决方案:
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;
}
推荐阅读
- java - 堆栈为空但仍然看到 EmptyStackException
- appium-android - centos8 install appium failed:errno ENOENT appium-windows-driver@1.18.1 install: `node install-npm.js`
- angular - Angular 路由页面成功但未显示 html 内容
- python - python显示一个实际上并不存在的错误
- dart - 在飞镖扩展名中有什么好处?
- powerbi - Power BI 中每个组中的筛选、分组依据和计算唯一值
- c - 我应该如何修改我的代码以不输出为 0?
- android - 如何在android中实现带有单位增加的虚线进度条
- azure-devops - 在 azure devops 仪表板上显示较少的代码覆盖率
- android - 输入'未来
' 不是类型 'List 的子类型 ?