Google Place Add API为C#客户端返回Invalid_Request

时间:2016-08-07 06:35:20

标签: google-maps dotnet-httpclient

这是我的C#代码:

private async Task<AddPlaceResponse> AddLocation(Place place)
        {
            AddPlaceResponse resp = new AddPlaceResponse();

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpContent contentPost = new StringContent(JsonConvert.SerializeObject(place), Encoding.UTF8, "application/json");

                    string url = string.Format("https://maps.googleapis.com/maps/api/place/add/json?key=MYKEY");

                    HttpResponseMessage response = await client.PostAsync(url, contentPost);

                    if (response.IsSuccessStatusCode)
                    {
                        var data =response.Content.ReadAsStringAsync();
                        var status = data.Result;
                        resp = JsonConvert.DeserializeObject<AddPlaceResponse>(status);

                    }

                }
            }
            catch (Exception ex)
            {
            }

            return resp;
        }

这是请求Json(放置对象):

{"location":{"lat":"22.5674722","lng":"88.3086388"},"accuracy":50,"name":"Prabartak Sangha","phone_number":"(+91) 8909878909","address":"Sitanath Banerjee Lane","types":["shoe_store"],"website":"https://www.google.co.in","language":"en"}

我无法理解为什么我总是以"Invalid_Request"为状态。

请帮忙。

1 个答案:

答案 0 :(得分:0)

请检查您的HTTP POST添加请求的请求正文。

Place Add - Status Codes中所述,

  

INVALID_REQUEST通常表示请求包含缺少的参数。尝试添加名称大于255个字符的位置时也会返回它。

正如Add a place中所述,请注意必需参数并且也有限制。

  • accuracy
  • address - (推荐)
  • language
  • location - (必填)
  • name - (必填且限制为255个字符)
  • phone_number - (推荐)
  • types - (必填)
  • website - (推荐)

<强>更新

不幸的是,Place Add已于2017年6月30日弃用,并将于2018年6月30日停止工作。因此您无法再使用此方法。有关详细信息,请参阅相应的geo blog post