调用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
相关产品推荐
相关产品推荐

