Reputation: 27
Up until recently, the following code was working and returned Travel Routes from the routing API.
string routesAPIurl = "https://routes.googleapis.com/directions/v2:computeRoutes";
WebRequest request = WebRequest.Create(routesAPIurl);
request.Method = "POST";
//request.Headers["Content-Type"] = "application/json";
request.ContentType = "application/json";
request.Headers["X-Goog-Api-Key"] = "***************************";
request.Headers["X-Goog-FieldMask"] = "routes.legs.steps.transitDetails";
//create transitObject
TransitObject transitO = new TransitObject();
transitO.travelMode = "TRANSIT";
transitO.departureTime = DateTime.Parse("2024-04-17T09:30:00.000Z");
transitO.computeAlternativeRoutes = true;
Origin trnOrigin = new Origin();
trnOrigin.address = "********";
transitO.origin = trnOrigin;
Destination trnDestination = new Destination();
trnDestination.address = "********";
transitO.destination = trnDestination;
TransitPreferences transitPreferences = new TransitPreferences();
transitPreferences.routingPreference = "TRANSIT_ROUTING_PREFERENCE_UNSPECIFIED";
transitPreferences.allowedTravelModes = new string[] { "LIGHT_RAIL", "BUS" }.ToList();
transitO.transitPreferences = transitPreferences;
var data = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(transitO));
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Here are the objects referred to in the code:
public class Destination
{
public string address { get; set; }
}
public class Origin
{
public string address { get; set; }
}
public class TransitObject
{
public Origin origin { get; set; }
public Destination destination { get; set; }
public string travelMode { get; set; }
public System.DateTime departureTime { get; set; }
public bool computeAlternativeRoutes { get; set; }
public TransitPreferences transitPreferences { get; set; }
}
public class TransitPreferences
{
public string routingPreference { get; set; }
public List<string> allowedTravelModes { get; set; }
}
As I run it today, it returns empty {}
Does anyone know why this would have suddenly stopped working? I cannot find anything on Google API docs that suggest updates or changes that would cause it to just suddenly return {}
Thanks in advance
I have checked my google api billing and balance is good. I have tried changing various fields with no luck. The code above was last confirmed working about a month ago.
Upvotes: 0
Views: 112
Reputation: 27
Ok I figured it out but I am leaving the question and Setting the answer to help anyone else who missed it.
Google will simply return 200 empty without much help, but I realised that the departure time was hardcoded, and needed to be set to always be in the future, as past travel routes are empty.
I have now written some code to generate a departure time always in the future and it is working again
Upvotes: 1