根据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、首先成为百度开发者,创建个工程

191508_ohNR_2811278.png

2、创建好如下

191556_20RO_2811278.png

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

203946_80Gm_2811278.png

二、拿到了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

205052_aMrA_2811278.png

转载于:https://my.oschina.net/AmosWang/blog/1612604

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值