Here is the code I write for getting the flight price information by using Google QPX Express API from Python:
import urllib2
import json
url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=AIzaSyBH_S3LDUQWmQtbXyExUShtUSI8MmxObfY"
code = {
"request": {
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": 1,
},
"slice": [
{
"kind": "qpxexpress#sliceInput",
"origin": "DCA",
"destination": "NYC",
"date": 2014-11-20,
}
],
"refundable": False,
"solutions": 5
}
}
jsonreq = json.dumps(code, encoding = 'utf-8')
req = urllib2.Request(url, jsonreq, {'Content-Type': 'application/json'})
flight = urllib2.urlopen(req)
response = flight.read()
flight.close()
print(flight)
It always give me the error of urllib2.HTTPError: HTTP Error 400: Bad Request
. I really can't figure out what to do.
Related: QPX Express API from Python
You're almost there! Just a few minor errors:
This gives:
By the way, to get the best responses, you should probably ask for more than 5 solutions. QPX Express tries to return a variety of answers (e.g., different times, different airlines, etc.) so if you want to have your own selection of the best trade-off of qualities, you probably want to examine more solutions.