[英]How to use an access token with the OpenCorporates REST API?
I am trying to access opencorporates.com. 我正在尝试访问opencorporates.com。 The page says that this is authenticated version for the GET method http://api.opencorporates.com/companies/gb/00102498?api_token=ab123cd45 . 该页面说明这是GET方法的身份验证版本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. 即使我不使用api令牌,我也可以使用api令牌收集相同数量的数据。 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¤t_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)
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
. 您的网址的结尾如下所示: ?api_token=ab123cd45
,但它已经位于网址的查询字符串部分中,因此它应该如下所示: &api_token=ab123cd45
。 (Replace the ?
with a &
.) (用&
替换?
)
You should consider using Requests when working with APIs. 在使用API时,您应该考虑使用请求 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.