Trying to connect to Google Routes API (URLConnection)

Hello all;

I have been using the WebMapviewer and the GraffitiWebMapViewer to retrieve information from the Google Maps platform. Here, I have a new need to connect without one of the two controls. (I am testing in a web app, but the plan is to execute this in a console application once I have iut nworking) Basically, just connect using URLConnection. I am unable to figure out how to incorporate my API key in the request. I tried many variations without success. The code below shows some such attempts. Can anyone point me in the right direction?

// This method will get a distance from the Google Routes API. Typically, the starting point is the official address and the destination is the arena.

Var LoSource As String = Officiel.Longitude
Var LaSource As String = Officiel.latitude
Var LoDest As String = Site.Longitude
Var LaDest As String = Site.latitude

Var GetDistance as New URLConnection
Var Key as string = "X-Goog-Api-Key: " + APIKeyG.left(APIKeyG.length-2)            // use same key as with webmapviewer
Var Request as string = """origin"":{""location"":{""latLng""{""latitude"": " + LaSource + ",""longitude"": " + loSource + "}}},""destination"":{""location"":{""latLng"":{""latitude"":" + LaDest +",""longitude"":" + LoDest + "}}},""travelMode"": ""DRIVE"",""routingPreference"": ""TRAFFIC_UNAWARE"",""computeAlternativeRoutes"": false,""routeModifiers"": {""avoidTolls"": false,""avoidHighways"": false,""avoidFerries"": false},""languageCode"": ""fr-CA"",""units"": ""METRIC""}"
Var Header as string = "application/json"

'GetDistance.SetRequestContent(Request, Header)
GetDistance.SetRequestContent(Request, Header + " &" + Key)
'GetDistance.SetRequestContent(Request, Key)
'Var response As String = GetDistance.SendSync("POST", "https://routes.googleapis.com/directions/v2:computeRoutes?"+ Key, 30)
'Var response As String = GetDistance.SendSync("POST", "https://routes.googleapis.com/directions/v2:computeRoutes&"+ Key, 30)
Var response As String = GetDistance.SendSync("POST", "https://routes.googleapis.com/directions/v2:computeRoutes", 30)

system.debuglog response

I either don’t get a response or I get an error 403. permission denied: Clearly, I am not passing the key properly.

{
“error”: {
“code”: 403,
“message”: “Method doesn’t allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API.”,
“status”: “PERMISSION_DENIED”
}
}

Now, I do have a key, it is the same key that works in the map viewers. The Key variable does contain the correct key.I seem to only need to understand the proper way of transmitting the key with the request.

All insights are welcome. Thank you in advance.

Try:

GetDistance.RequestHeader("X-Goog-Api-Key") = APIKeyG.left(APIKeyG.length-2)
GetDistance.SetRequestContent(Request, "application/json")
1 Like

Thank you Andrew, that was so simple actually…

I am connecting now. Your answer allowed me to also figure out how to pass field masks.

the code looks like this now:

// This method will get a distance from the Google Routes API. Typically, the starting point is the official address and the destination is the arena.

Var LoSource As String = Officiel.Longitude
Var LaSource As String = Officiel.latitude
Var LoDest As String = Site.Longitude
Var LaDest As String = Site.latitude

Var GetDistance as New URLConnection
Var Request as string = """origin"":{""location"":{""latLng""{""latitude"": " + LaSource + ",""longitude"": " + loSource + "}}},""destination"":{""location"":{""latLng"":{""latitude"":" + LaDest +",""longitude"":" + LoDest + "}}},""travelMode"": ""DRIVE"",""routingPreference"": ""TRAFFIC_UNAWARE"",""computeAlternativeRoutes"": false,""routeModifiers"": {""avoidTolls"": false,""avoidHighways"": false,""avoidFerries"": false},""languageCode"": ""fr-CA"",""units"": ""METRIC""}"

GetDistance.RequestHeader("X-Goog-Api-Key") = APIKeyG.left(APIKeyG.length-2)
GetDistance.RequestHeader("X-Goog-FieldMask") = "routes.distanceMeters"
GetDistance.SetRequestContent(Request, "application/json")

Var response As String = GetDistance.SendSync("POST", "https://routes.googleapis.com/directions/v2:computeRoutes", 10)


system.debuglog response

Now, I need to work on my json payload. It is malformed. A comparatively minor issue now that I can connect. At first glance, I am missing a colon after latLNG. Detailed work, but not difficult. Probably best left for tomorrow morning.

Thank you again!

LD