My day hacking Google QPX Express airfare API

qpx express When my classmate and I previously attempted to use the Google Feed API for our group project, I have realized that Google documentation is not great for “beginners,” in our case inexperienced developers. It seems to be simple in theory, make a HTTP post request to this url 'https://www.googleapis.com/qpxExpress/v1/trips/search' with json in the request body. But the learning curve is high and here are the struggles I’ve gone through:

1. API key setup: I think this is worth mentioning because the page to do this isn’t straight forward. Sign into Google developers console, click on the project/app => APIs and Auth => Credentials => Click on the Credentials tab. Here you can add and remove API keys.

3. Ruby wrapper: My mentor suggested that I could use Google’s Ruby wrapper for the API so I can make the request server-side. I was able to find documentation on how to create the API client (which is common to all of Google’s APIs). BUT there is one line of documentation specific to QPX, and I couldn’t figure out how to specify the request body so I gave up.

3. 403 “Access not configured”: I put my API key in the header as instructed but was getting this error. It was a combination of 2 things. I solved my problem but didn’t know which fix did it.
+ Initially I had specified under allowed IP the IP address of my apartment’s internet access but I was working from another one. This post suggests that you have to register your v6 and not v4 IP. I don’t even know what that is so I just leave it blank as one post recommends to allow requests from any IP.

+ Still getting the error. I had to restart my rails server and it changed to a 400 status code.

3. 400 “Bad request”: I was fumbling for a while because I didn’t know how to investigate what went wrong. Luckily going under the Network tab (Chrome developer console)
Parse Error. This API does not support parsing form-encoded input

With the help of this stackoverflow thread, I set contentType: 'application/json' in my ajax request.

4. 200 with empty response body: for a while I got a 200 status with nothing in the response body. Even more unhelpful than the two previous errors. I don’t know why this went through, but I started hitting the API and encountered 400 again.
Update: According a commenter, empty 200s happen when using AA or DL as preferred airlines. You need specific approval from those airlines in order to get the information.

5. 400 “Parse error”: This error is similar to the third one but I figured the cause was different because of the different message. It was a bit vague and difficult to search for but I finally found this thread that nails the problem in the head. My json was not really json but a javascript object, very close but not really. I had to simply use JSON.stringify(object) to turn it into valid json. The reason I was so slow to figure out this issue (nearly 1 hour) is because a) I’ve mainly used the opposite method, JSON.parse to read response body and rarely done a post request with such a big request body; b) I’ve mostly made ajax requests to my own Rails server, which accepts an object with no problem.

The “victory” wasn’t that great but it was really, really worthwhile to get back the response object in the console since I’ve spent all day hacking this API with basically no help. I guess I helped myself to Google and Stackoverflow.

qpx-response

Advertisements

14 thoughts on “My day hacking Google QPX Express airfare API

  1. George says:

    Thanks for the post. I am having hte same 403 issue you are having. Can you guide me to where I am supposed to put my ip v6? I can’t find it in the google console.

    • Hi George, I don’t think you need to use IPv6. To edit allowed IPs, go to APIs & Auth on the sidebar of Developers Console => Credentials => Edit allowed IPs. I left mine blank to allow all IPs.

  2. George says:

    Thanks for the quick reply. I don’t have that option. I am building this app using an Android key. Could that be the issue?

    • I’m not very familiar with Android development. But it seems to require certificate fingerprints instead of IP addresses:

      ACCEPT REQUESTS FROM AN ANDROID APPLICATION WITH ONE OF THE CERTIFICATE FINGERPRINTS AND PACKAGE NAMES LISTED BELOW
      One SHA1 certificate fingerprint and package name (separated by a semicolon) per line. Example: 45:B5:E4:6F:36:AD:0A:98:94:B4:02:66:2B:12:17:F2:56:26:A0:E0;com.example

  3. Sound strange. I had no single problem with implementing usage of QPX Express in my project. Looks like you’ve used Google API first time, so you had to fine right libraries, test how it works etc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s