You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Place API无法返回20+结果,分页调用返回INVALID_REQUEST求助

解决Google Places API附近搜索分页返回INVALID_REQUEST的问题

我正在学习Xamarin Forms,使用Google Place API的附近搜索功能,已在.NET中成功调用API,但按Google文档仅能获取20条结果。我已成功拿到next_page_token,但将其加入参数调用第二页时返回INVALID_REQUEST,results为空。

原代码

string lati = location.Latitude.ToString().Replace(",", ".");
string longi = location.Longitude.ToString().Replace(",", ".");

string latitude = lati;
string longitude = longi;
string Myradius = "3000";

var URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?";
string urlParameters = "?location=" + latitude + "," + longitude + "&radius=" + Myradius + "&key=My_GOOGLE_KEY";

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);

// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

// List data response.
HttpResponseMessage response = await client.GetAsync(urlParameters);

if (response.IsSuccessStatusCode)
{
    JObject joResponse = JObject.Parse(await response.Content.ReadAsStringAsync());

    JArray MyJsonResult = (JArray)joResponse["results"];

    // I want to have the second page result of my API Call
    string token = joResponse["next_page_token"].ToString(); // I have manage to get the token
    JArray MyJsonResult2 = await GetnextPageData(latitude, longitude, Myradius, token); //no result
}

async Task<JArray> GetnextPageData(string latitude, string longitude, string Myradius , string token)
{
    var URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?";

    string urlParameters = "?location=" + latitude + "," + longitude + "&radius=" + Myradius + "&key=My_GOOGLE_KEY&pagetoken=" + token;

    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(URL);

    // Add an Accept header for JSON format.
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // List data response
    HttpResponseMessage response = await client.GetAsync(urlParameters);
    Console.WriteLine("MyPrint " + response);

    if (response.IsSuccessStatusCode)
    {
        JObject joResponse = JObject.Parse(await response.Content.ReadAsStringAsync());
        Console.WriteLine("MyPrint " + response);

        JArray MyJsonResult = (JArray)joResponse["results"];

        if (MyJsonResult != null)
        {
            return MyJsonResult;
        }
        else
        {
            return null;
        }
    }

    return null;
}

错误响应内容

第二页调用返回状态码200,但响应体为:

{
  "html_attributions": [],
  "results": [],
  "status": "INVALID_REQUEST"
}

问题原因

  1. URL格式错误:原代码中URL已包含?,但urlParameters又以?开头,导致最终URL出现双问号(??),触发格式错误。
  2. 分页参数冗余:使用pagetoken请求分页时,Google API要求仅传递key和pagetoken,不需要重复传入location、radius等原始搜索参数,否则会判定为无效请求。
  3. 调用时机过早:next_page_token生成后,Google服务器需要几秒同步分页数据,立即调用会导致token未生效。

修复后的代码

string lati = location.Latitude.ToString().Replace(",", ".");
string longi = location.Longitude.ToString().Replace(",", ".");

string latitude = lati;
string longitude = longi;
string Myradius = "3000";

var baseUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json";
string urlParameters = $"location={latitude},{longitude}&radius={Myradius}&key=My_GOOGLE_KEY";

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

// 发起首次搜索请求
HttpResponseMessage response = await client.GetAsync($"{baseUrl}?{urlParameters}");

if (response.IsSuccessStatusCode)
{
    JObject joResponse = JObject.Parse(await response.Content.ReadAsStringAsync());
    JArray MyJsonResult = (JArray)joResponse["results"];

    // 检查是否有分页token
    if (joResponse["next_page_token"] != null)
    {
        string token = joResponse["next_page_token"].ToString();
        // 等待2秒确保token生效
        await Task.Delay(2000);
        JArray MyJsonResult2 = await GetnextPageData(token);
    }
}

async Task<JArray> GetnextPageData(string token)
{
    var baseUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json";
    // 分页请求仅需传递key和pagetoken
    string urlParameters = $"key=My_GOOGLE_KEY&pagetoken={token}";

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

    HttpResponseMessage response = await client.GetAsync($"{baseUrl}?{urlParameters}");

    if (response.IsSuccessStatusCode)
    {
        JObject joResponse = JObject.Parse(await response.Content.ReadAsStringAsync());
        string status = joResponse["status"].ToString();
        
        if (status == "OK")
        {
            return (JArray)joResponse["results"];
        }
        else
        {
            Console.WriteLine($"分页请求错误:{status}");
            return null;
        }
    }

    return null;
}

额外注意事项

  • next_page_token仅在短时间内有效(1-2分钟),且只能使用一次。
  • 注意Google Places API的请求频率限制,避免短时间内多次调用触发OVER_QUERY_LIMIT。
  • 建议复用同一个HttpClient实例,减少资源消耗。

内容的提问来源于stack exchange,提问作者hugokis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 08:56:07