How to use Trade Me’s API with Python

TradeMe has an excellent API that allows users to pull all sorts of data from their site, such as car or real estate listings. As a Python beginner, I was a bit intimidated by Python’s API and the authentication process. However, it turned out to be very easy to use with the help of Python’s “OAuth1Session” library. 

Step 1: Register your program with TradeMe

All you need to do to get started is sign into TradeMe and fill out this form on their website: https://www.trademe.co.nz/MyTradeMe/Api/RegisterNewApplication.aspx
TradeMe got back to me within a couple days and approved my application. Once approved, you can see your consumer Key and Secret at: https://www.trademe.co.nz/MyTradeMe/Api/DeveloperOptions.aspx

Step 2: Authenticate yourself 

In order to request information from TradeMe, you need to authenticate yourself so TradeMe knows who is asking for data. Although it may be tedious and has to be done manually, I found it easiest to authenticate myself using their web-page: https://developer.trademe.co.nz/api-overview/authentication/

Simply put in your consumer key and token, select “Production” for the environment (so you get data from the real TradeMe, rather than their fake sandbox site for testing purposes), and select the permissions you need. Generate your secret Key and Token and copy them into a Python script. 

Keep your consumer and secret Key, Token, and Secrets private! Otherwise, anyone could impersonate you on TradeMe. 

Step 3: Using Python to make requests

In order to request data from TradeMe, we need to make authenticated searches to their website. I am using the Python library “requests_oauthlib” to handle all the heavy lifting. TradeMe uses OAuth 1.0 to handle authentication. Since TradeMe uses OAuth1.0, we can use “OAuth1Session” from “requests_oauthlib”. We pass in our consumer key, token, and secrets into “OAuth1Session” and start making some requests! 

Example search

I’m interested in buying a new car but don’t want to pay too much. I could manually comb through all of TradeMe’s car listings and get a general idea of the price of cars I’m interested in are. OR I could have Python do it for me and get the exact values of specific makes and models. Here’s example code which uses OAuth1Session to ask for all the current car listings on TradeMe in JSON format then stores them using Pandas. You can make your own TradeMe searches here: https://developer.trademe.co.nz/api-reference/search-methods/

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.