How to use an access token with the OpenCorporates REST API?

745 views Asked by At

I am trying to access opencorporates.com. The page says that this is authenticated version for the GET method http://api.opencorporates.com/companies/gb/00102498?api_token=ab123cd45.

This is my code to access the dataset. Here I am changing the jurisdiction code through the codes that I collected in a file. Even if I don't use the api token I am able to collect the same amount of data that I can with the api token. Am I doing something wrong here?

import urllib2
import json,os

f = open('codes','r')
for line in f.readlines():
   id = line.strip('\n')
   url = 'http://api.opencorporates.com/v0.2/companies/search?q=&jurisdiction_code={0}&per_page=26&current_status=Active&page={1}?api_token=ab123cd45' 
   i = 0
   directory = id
   os.makedirs(directory)
   while True:
      i += 1
      req = urllib2.Request(url.format(id, i))
      print url.format(id,i)
      try:
         response = urllib2.urlopen(url.format(id, i))
      except urllib2.HTTPError, e:
        break
      content = response.read()
      fo = str(i) + '.json'    
      OUTFILE = os.path.join(directory, fo)
      with open(OUTFILE, 'w') as f:
          f.write(content)
1

There are 1 answers

0
davidism On BEST ANSWER

The end of your url looks like this: ?api_token=ab123cd45, but it's already in the query string portion of the url, so it should look like: &api_token=ab123cd45. (Replace the ? with a &.)

You should consider using Requests when working with APIs.