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

调用Google Photos mediaItems:search接口遇400错误,如何按日期范围过滤?

问题描述

调用Google Photos Library API的mediaItems:search接口获取图片列表时,不带POST请求体可正常返回结果,但添加日期范围过滤的请求体后,收到400 Bad Request错误。

调用代码如下:

string requestUri = "https://photoslibrary.googleapis.com/v1/mediaItems:search";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(requestUri);
webRequest.Method = "POST";
webRequest.Headers.Add(string.Format("Authorization: Bearer {0}", accessToken));
webRequest.ContentType = "application/json";

string bodyData = "{\"pageSize\":25,\"filters\":{\"dateFilter\":{\"ranges\":[{\"startdate\":{\"year\":2023,\"month\":5,\"day\":4},\"endDate\":{\"year\":2023,\"month\":6,\"day\":4}}]}},\"pageToken\":\"\"}";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytes = encoding.GetBytes(bodyData);
webRequest.ContentLength = bytes.Length;
using (Stream requestStream = webRequest.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
}
WebResponse userinfoResponse = await webRequest.GetResponseAsync();

返回错误信息:

The remote server returned an error: (400) Bad Request.

解决方案

导致400错误的核心问题有两个:

  • JSON字段名大小写不匹配:Google API要求驼峰命名,你将startDate误写为startdate,API无法识别该字段。
  • 编码方式错误:JSON内容需使用UTF-8编码,ASCII编码无法处理非ASCII字符,可能导致请求体解析失败。

另外,空的pageToken可以直接省略,无需传入空字符串。

修改后的HttpWebRequest代码

string requestUri = "https://photoslibrary.googleapis.com/v1/mediaItems:search";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(requestUri);
webRequest.Method = "POST";
webRequest.Headers.Add(string.Format("Authorization: Bearer {0}", accessToken));
// 明确指定UTF-8编码
webRequest.ContentType = "application/json; charset=utf-8";

// 修正startdate为startDate,移除空pageToken
string bodyData = "{\"pageSize\":25,\"filters\":{\"dateFilter\":{\"ranges\":[{\"startDate\":{\"year\":2023,\"month\":5,\"day\":4},\"endDate\":{\"year\":2023,\"month\":6,\"day\":4}}]}}}";
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(bodyData);
webRequest.ContentLength = bytes.Length;
using (Stream requestStream = webRequest.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
}
WebResponse userinfoResponse = await webRequest.GetResponseAsync();

推荐使用HttpClient(更简洁的现代API)

using System.Net.Http;
using System.Text;
using Newtonsoft.Json;

var httpClient = new HttpClient();
// 设置Authorization头
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);

// 用匿名对象构建请求体,避免手动拼接JSON的错误
var requestBody = new
{
    pageSize = 25,
    filters = new
    {
        dateFilter = new
        {
            ranges = new[]
            {
                new
                {
                    startDate = new { year = 2023, month = 5, day = 4 },
                    endDate = new { year = 2023, month = 6, day = 4 }
                }
            }
        }
    }
};

// 序列化对象为JSON,自动处理编码和字段名
string jsonBody = JsonConvert.SerializeObject(requestBody);
var requestContent = new StringContent(jsonBody, Encoding.UTF8, "application/json");

HttpResponseMessage response = await httpClient.PostAsync(requestUri, requestContent);
// 自动抛出HTTP错误(如400、401等),便于调试
response.EnsureSuccessStatusCode();
string responseContent = await response.Content.ReadAsStringAsync();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 06:57:01