Get user address according to ip - Baidu

Baidu's Api: GET request

https://openapi.baidu.com/rest/2.0/iplib/query?access_token={access_token}&ip={ip}

This is mine. You can measure:

https://openapi.baidu.com/rest/2.0/iplib/query?access_token=23.8c3b9a489ff5f9b276ae49a54166fecc.2592000.1518836471.2307466515-10709969&ip=116.226.62.127

The results returned are as follows:

{"116.226.62.127":{"province":"\u4e0a\u6d77","city":"\u4e0a\u6d77"}}

1, How to get access_token

There is an access Ou token in the link, which literally means a user access token. How can it come from?

1. First, become a Baidu developer and create a project

2. Create the following

3. access_token

Baidu will not explicitly give access_token. One way to speculate is to get it according to API KEY:

General browser get request

https://openapi.baidu.com/oauth/2.0/authorize

? response? Type = token & client? Id = {your API key} & redirect? URI = OOB & scope = netdisk

After the request is successful, the link on the browser will be changed and access_token will appear

2, Get access_token and parse the data in the code

1. That's the key code

JSON: import com.alibaba.fastjson.JSON;

**********************************************************************************

/**
 * Baidu visit token
 */
private static final String ACCESS_TOKEN = "23.8c3b9a489ff5f9b276ae49a54166fecc.2592000.1518836471.2307466515-10709969";

private static final RestTemplate REST_TEMPLATE = new RestTemplate();

private static String requestGetType(String url, Map<String, Object> uriVariables) {
    return REST_TEMPLATE.getForObject(url, String.class, uriVariables);
}

/**
 * Get location according to ip [Baidu]
 *
 * @param ip ip
 * @return BaiDuLocationDTO
 */
public static GeneralResponse<BaiDuLocationDTO> getLocationByBaiDu(String ip) {
    String url = "https://openapi.baidu.com/rest/2.0/iplib/query?access_token={access_token}&ip={ip}";
    Map<String, Object> uriVariables = new HashMap<>(2);
    try {
        uriVariables.put("access_token", URLEncoder.encode(ACCESS_TOKEN, "utf-8"));
        uriVariables.put("ip", URLEncoder.encode(ip, "utf-8"));
    } catch (UnsupportedEncodingException e) {
        return GeneralResponse.FAIL;
    }
    String result = requestGetType(url, uriVariables);
    BaiDuLocationDTO successObject = JSON.parseObject(result).getObject(ip, BaiDuLocationDTO.class);
    if (successObject == null) {
        BaiDuLocationErrorDTO object = JSON.parseObject(result, BaiDuLocationErrorDTO.class);
        if ("110".equals(object.getErrorCode())) {
            return new GeneralResponse<>(UserExceptionEnum.BAI_DU_ERROR_TOKEN_NOT_EXIST);
        } else if ("100".equals(object.getErrorCode())) {
            return new GeneralResponse<>(UserExceptionEnum.BAI_DU_ERROR_PARAMETER, object.getErrorMsg());
        } else if ("2".equals(object.getErrorCode())) {
            return new GeneralResponse<>(UserExceptionEnum.BAI_DU_ERROR_SYSTEM_UNAVAILABLE);
        } else {
            return new GeneralResponse<>(UserExceptionEnum.BAI_DU_ERROR_OTHER, object.getErrorMsg());
        }
    }
    return new GeneralResponse<>(successObject);
}

***************************************************************************************

public class BaiDuLocationDTO implements Serializable {
    private static final long serialVersionUID = 1115816313136321458L;
    /**
     * Province
     */
    private String province;
    /**
     * City
     */
    private String city;

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}

2. For details, please refer to the following items:

https://github.com/AmosWang0626/boot-single/tree/develop

Tags: JSON REST github

Posted on Tue, 05 May 2020 02:22:40 -0400 by shaunie123