您的位置:首页 > 其它

根据ip获取用户地址-百度

2018-01-24 00:00 381 查看
摘要: 百度API方式通过ip获取用户地址,还有淘宝、新浪等等方式

百度的Api:GET请求
https://openapi.baidu.com/rest/2.0/iplib/query?access_token={access_token}&ip={ip}
这是我的,可以测下:

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

返回的结果如下:

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

一、如何获取access_token

链接中有个access_token,字面意思就是用户访问令牌,怎么来的呢?

1、首先成为百度开发者,创建个工程



2、创建好如下



3、拿到access_token

百度不会显式给出access_token,有种投机的方法就是根据API KEY拿到:

通用浏览器get请求
https://openapi.baidu.com/oauth/2.0/authorize
?response_type=token&client_id={你的API KEY}&redirect_uri=oob&scope=netdisk

请求成功之后,浏览器上的链接就会变掉,并出现access_token



二、拿到了access_token,代码中解析数据即可

1、关键代码就这些

JSON: import com.alibaba.fastjson.JSON;

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

/**
* 百度访问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);
}

/**
* 根据ip获取位置【百度】
*
* @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;
/**
* 省份
*/
private String province;
/**
* 城市
*/
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、具体可参考项目:

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: