0

I am trying to call the Google API for mediaItems:search to return a list of images. However I'm getting error 400 bad request back. If I don't set the body of the POST at all, I successfully get back images, but I need to filter them. How can make a successful call, filtering by date range?

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();

Response is

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
BrianTW
  • 43
  • 1
  • 3
  • `\"pageToken\":\"\"` why is that there? Just don't pass it. Side note: not that it will fix your issue but `HttpWebRequest` is basically deprecated, you should use the newer `HttpClient`. And use a proper JSON serializer. Better yet, just use a proper gRPC library – Charlieface Jun 04 '23 at 03:30

1 Answers1

0

The problem was that the JSON for the filter is case sensitive. I had 'startdate' when it should have been 'startDate'.

https://developers.google.com/photos/library/reference/rest/v1/mediaItems/search

BrianTW
  • 43
  • 1
  • 3