c# - 如何将客户的 IP 地址转发到 Google Places Autocomplete API

标签 c# asp.net google-api google-places-api

我从网络服务器调用 Google Places Autocomplete API,因此当客户端代码无法从浏览器捕获纬度/经度时,默认情况下位置偏差将始终基于我的服务器的 IP 地址。我正在尝试找到一种方法将客户端的 IP 地址传递给 Google,而不是使用我的 IP 地址。我已经在服务器 API 上捕获了客户端 IP,但似乎没有任何方法可以在自动完成请求中转发该 IP。

Google 在文档中指出:

Note: If you do not supply the location and radius, the API will attempt to detect the user's location from their IP address, and will bias the results to that location.

我尝试向自动完成请求添加不同的 header ,但它们都被忽略,并且在我的服务器 IP 上仍然存在偏差结果。也许我还没有找到正确的标题。

//pseudo C#
headers.Add("X-ProxyUser-Ip", "172.217.5.228");
headers.Add("X-Forwarded-For", "172.217.5.228");

是否有正确的方法将客户端 IP 从我的服务器转发到 Google API?

源代码:https://developers.google.com/places/web-service/autocomplete#location_biasing

最佳答案

目前,Google Places 服务器端 API 不支持将 IP 地址作为位置偏差的参数。所以你有两个选择。

  1. 使用客户端 API(您已经提到这不可行)
  2. 调用反向地理定位 API 以获取 IP 的国家/地区代码。 然后通过组件参数将此国家/地区代码传递给 Google Places API。

Ipfind 服务示例

using (var webClient = new System.Net.WebClient()) {
  string ip_address = "10.23.45.154";
  string URL = "https://api.ipfind.com/?ip=" + ip_address;

  var json = webClient.DownloadString(URL);
  // extract ISO country code here and call Google Places API
}

components — A grouping of places to which you would like to restrict your results. Currently, you can use components to filter by up to 5 countries. Countries must be passed as a two character, ISO 3166-1 Alpha-2 compatible country code. For example: components=country:fr would restrict your results to places within France. Multiple countries must be passed as multiple country:XX filters, with the pipe character (|) as a separator. For example: components=country:us|country:pr|country:vi|country:gu|country:mp would restrict your results to places within the United States and its unincorporated organized territories.

关于c# - 如何将客户的 IP 地址转发到 Google Places Autocomplete API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56604236/

相关文章:

c# - Entity Framework 5 Code First 自引用关系

C# 或 C++ 如何使程序在询问用户密码时运行?

c# - 窗口调整大小处理事件

c# - 我可以从项目中获取渲染参数吗?

authentication - 使用 Google Storage Transfer API 将数据从外部 GCS 传输到我的 GCS

google-maps-api-3 - 来自 Amazon EC2 的地理定位请求

c# - XAML/C# API 中是否包含 Javascript API 中的所有功能?

返回语句后的 C# 触发事件

asp.net - 一个页面只能有一个服务器端表单标签..但我需要 2 个

javascript - 未捕获的 InvalidValueError : not a LatLngBounds or LatLngBoundsLiteral: unknown property f