Issue with outputting tolls using Google routes API

91 Views Asked by At

I am able to get toll information correctly using Google routes API when running the code on localhost.

Response on localhost:

{'routes': [{'legs': [{'travelAdvisory': {'tollInfo': {'estimatedPrice': [{'currencyCode': 'INR', 'units': '170'}]}}}], 'distanceMeters': 112558, 'duration': '7729s', 'travelAdvisory': {'tollInfo': {'estimatedPrice': [{'currencyCode': 'INR', 'units': '170'}]}}}]}

When the same code is migrated to the cloud server, the toll information becomes blank.

Response on cloud server:

{'routes': [{'legs': [{}], 'distanceMeters': 112558, 'duration': '7729s'}]}

I was expecting the same response on the cloud server since the code is the same.

My code for your reference:

`def FindToll(from_address,to_address):
   #  
   google_api_key =  config('API_key')
                        
   url = "https://routes.googleapis.com/directions/v2:computeRoutes"

   payload =  json.dumps(  {
   "origin":{
      "address": from_address
     
   },
   "destination":{
      "address": to_address
       
   },  
   "travelMode": "DRIVE",
   "extraComputations": ["TOLLS"],
   "routeModifiers":{
      "vehicleInfo":{
         "emissionType": "GASOLINE"
      },
      "tollPasses": [
         "IN_FASTAG"
      ]
   }
   })
   
   headers = {
      "Content-Type": "application/json",
      "X-Goog-Api-Key": google_api_key,
      "X-Goog-FieldMask":"routes.duration,routes.distanceMeters,routes.travelAdvisory.tollInfo,routes.legs.travelAdvisory.tollInfo"
   }
    
   
    response = requests.post(url, data=payload, headers=headers)
        #    print( response)
    res= response.json()
    
 
   #   print(res)
      
   if res.get('routes') :
       if not res.get('routes')[0].get('legs')[0]:
            return 0
       else:
            toll_cost=res.get('routes')[0].get('legs')[0].get('travelAdvisory')['tollInfo']['estimatedPrice'][0]['units']
            return int(toll_cost)
   return 0 `
     
0

There are 0 best solutions below