企查查开放平台API使用

237 阅读2分钟

使用企查查开放平台 API 的步骤如下:

  1. 注册与申请

    • 访问官网:首先访问企查查开放平台的官方网站openapi.qcc.com/
    • 注册账号:在网站上进行注册,提供有效的联系方式和基本信息等。
    • 申请 API 密钥:注册完成后,平台会为你生成一个独有的 API 密钥,这是使用企查查开放平台服务的凭证,务必妥善保管。
  2. 选择 API 服务:企查查开放平台提供了多种 API 接口,比如企业基本信息查询、股东信息查询、法律诉讼查询等。根据自己的业务需求选择合适的 API 服务,并仔细阅读相关的接口文档,了解接口的功能、参数要求以及返回数据的格式等1。

  3. API 调用

    • 设置请求头:构造请求头,请求头中一般包含两个重要字段,一个是 “token”,另一个是 “timespan”。“timespan” 是精确到秒的时间戳;“token” 则是将你的 API 密钥、精确到秒的时间戳以及平台分配给你的密钥三个东西拼接,然后使用 MD5 加密算法进行加密,加密后的字符串再全部转化为大写。
    • 发起请求:使用你熟悉的编程语言和工具,根据选定的 API 接口地址和请求参数,发起 HTTP 请求。例如,在 Java 中可以使用 HttpClient 类,在 Python 中可以使用 requests 库等。将 API 密钥等信息添加到请求中,以获取企业数据。
  4. 数据解析与应用

    • 接收响应:成功发起请求后,会收到企查查开放平台返回的响应数据。根据接口文档中说明的返回数据格式,对响应数据进行解析。如果返回的是 JSON 格式的数据,可以使用相应编程语言中的 JSON 解析库来提取所需的信息。
    • 应用数据:将解析后的数据应用到自己的业务系统或应用中,比如进行企业信息展示、数据分析、风险评估等业务场景。

    package org.ld.httpGetDemo;

    import static java.lang.System.out;

    import java.io.IOException; import java.util.regex.Pattern;

    import org.apache.commons.codec.digest.DigestUtils; import org.apache.http.client.methods.HttpHead; import org.json.JSONException; import org.json.JSONObject;

    import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper;

    public class MainApp {

    private static final String appkey = "appkey";
    private static final String secretKey = "secretKey";
    
    public static void main(String[] args) {
    	String reqInterNme = "http://api.qichacha.com/XXXXXX";
    	String paramStr = "keyword=企查查科技有限公司";
    	String status = "";
    	try {
    		HttpHead reqHeader = new HttpHead();
    		String[] autherHeader = RandomAuthentHeader();
    		reqHeader.setHeader("Token", autherHeader[0]);
    		reqHeader.setHeader("Timespan", autherHeader[1]);
    		final String reqUri = reqInterNme.concat("?key=").concat(appkey).concat("&").concat(paramStr);
    		String tokenJson = HttpHelper.httpGet(reqUri, reqHeader.getAllHeaders());
    		out.println(String.format("==========================>this is response:{%s}", tokenJson));
    
    		status = FormartJson(tokenJson, "Status");
    		out.println(String.format("==========================>Status:{%s}", status));
    		if (!HttpCodeRegex.isAbnornalRequest(status)) {
    			PrettyPrintJson(tokenJson);
    		}
    	} catch (Exception e1) {
    		e1.printStackTrace();
    	}
    }
    
    // 获取返回码 Res Code
    static class HttpCodeRegex {
    	private static final String ABNORMAL_REGIX = "(101)|(102)";
    	private static final Pattern pattern = Pattern.compile(ABNORMAL_REGIX);
    	protected static boolean isAbnornalRequest(final String status) {
    		return pattern.matcher(status).matches();
    	}
    }
    
    // 获取Auth Code
    protected static final String[] RandomAuthentHeader() {
    	String timeSpan = String.valueOf(System.currentTimeMillis() / 1000);
    	String[] authentHeaders = new String[] { DigestUtils.md5Hex(appkey.concat(timeSpan).concat(secretKey)).toUpperCase(), timeSpan };
    	return authentHeaders;
    }
    
    // 解析JSON
    protected static String FormartJson(String jsonString, String key) throws JSONException {
    	JSONObject jObject = new JSONObject(jsonString);
    	return (String) jObject.get(key);
    }
    
    // pretty print 返回值
    protected static void PrettyPrintJson(String jsonString) throws JSONException {
    	try {
    		ObjectMapper mapper = new ObjectMapper();
    		Object obj = mapper.readValue(jsonString, Object.class);
    		String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
    		out.println(indented);
    	} catch (JsonProcessingException e) {
    		e.printStackTrace();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    }
    

    }