cop*_*jon 6 .net c# google-maps geocoding google-maps-api-3
因此,当我导入记录时,我正在编写一个缓存地理编码数据的应用程序.当我使用未签名的请求时,我的工作正常,但是当我尝试使用我公司的clientid和签名时,我似乎无法弄清楚出了什么问题.我总是得到一个403 Forbidden.
这是我的URL构建器:
private const string _googleUri = "http://maps.googleapis.com/maps/api/geocode/xml?address=";
private const string _googleClientId = "XXXXXXXX";
private const string _googleSignature = "XXXXXXXXXXXXXXXXXXXXXXXX";
//RESOLVED
private static String GetGeocodeUri(string address)
{
ASCIIEncoding encoding = new ASCIIEncoding();
string url = String.Format("{0}{1}&client={2}&sensor=false"
, _googleUri
, HttpUtility.UrlEncode(address)
, _googleClientId);
// converting key to bytes will throw an exception, need to replace '-' and '_' characters first.
string usablePrivateKey = _googleSignature.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;
}
Run Code Online (Sandbox Code Playgroud)
这是该计划:
public static AddressClass GetResponseAddress(string address)
{
AddressClass GoogleAddress = new AddressClass();
XmlDocument doc = new XmlDocument();
String myUri = GetGeocodeUri(address);
try
{
doc.Load(myUri);
XmlNode root = doc.DocumentElement;
if (root.SelectSingleNode("/GeocodeResponse/status").InnerText == "OK")
{
GoogleAddress.Latitude = Double.Parse(root.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText);
GoogleAddress.Longitude = Double.Parse(root.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception <" + ex.Message + ">");
}
return GoogleAddress;
}
Run Code Online (Sandbox Code Playgroud)
现在,我对它不起作用的最初反应是Google必须错过引用域,因为它们必须注册.所以我尝试使用HttpWebRequest并将引用设置为我的域,但仍然没有骰子.
//Not needed, Just an alternate method
public static AddressClass GetResponseAddress(string address)
{
AddressClass GoogleAddress = new AddressClass();
WebClient client = new WebClient();
XmlDocument doc = new XmlDocument();
Uri myUri = new Uri(GetGeocodeUri(address));
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(myUri);
myRequest.Referer = "http://www.myDomain.com/";
//I've even tried pretending to be Chrome
//myRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7";
try
{
doc.Load(myRequest.GetResponse().GetResponseStream());
XmlNode root = doc.DocumentElement;
if (root.SelectSingleNode("/GeocodeResponse/status").InnerText == "OK")
{
GoogleAddress.Latitude = Double.Parse(root.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText);
GoogleAddress.Longitude = Double.Parse(root.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception <" + ex.Message + ">");
}
return GoogleAddress;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.
const String gmeClientID = "gme-myClientId";
const String key = "myGoogleKey";
var urlRequest = String.Format("/maps/api/geocode/json?latlng={0},{1}&sensor=false&client={2}",Latitude,Longitude,gmeClientID);
HMACSHA1 myhmacsha1 = new HMACSHA1();
myhmacsha1.Key = Convert.FromBase64String(key);
var hash = myhmacsha1.ComputeHash(Encoding.ASCII.GetBytes(urlRequest));
var url = String.Format("http://maps.googleapis.com{0}&signature={1}", urlRequest, Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_"));
var request = (HttpWebRequest)HttpWebRequest.Create(url);
Run Code Online (Sandbox Code Playgroud)
URL 编码有时是必要的(见下文),但还不够。您的问题是您实际上并没有签署您的请求。
常量中的值_googleSignature不是签名,而是您的私有密钥,这是不好的。您的私人加密密钥永远不应该成为任何请求的一部分。
相反,您需要使用它为每个独特的请求生成新的签名。请参阅用于业务身份验证的 Maps API文档,它还包括在 Java 中签署 URL 的示例:)
使用您的 Maps API for Business 客户端 ID 和您的私有加密密钥签署对 Google Maps API Web 服务的请求时,Referer 标头和源 IP 地址完全无关;)
URL 编码仅在address参数上是必需的,作为构建有效 URL 的一部分。您永远不应该对您的签名进行 URL 编码,因为通过对 URL 使用修改后的 Base64,它已经是 URL 安全的。
| 归档时间: |
|
| 查看次数: |
5695 次 |
| 最近记录: |