OAuth 授权链接里的回调地址参数丢失

OAuth 的授权链接里有一个回调地址的参数:

https://developers.e.qq.com/oauth/authorize?client_id=123456&redirect_uri=https://www.baidu.com?platform=jiaobuchong&name=jack&state=&scope=ADS_MANAGEMENT

这个授权链接里的回调地址参数是:

https://www.baidu.com?platform=jiaobuchong&name=jack

授权后地址变成了:

https://www.baidu.com?platform=jiaobuchong&auth_code=xxxxxxx

缺失了参数 name=jack,解决方法就是对回调地址中的 query 参数 :platform=jiaobuchong&auth_code=xxxxxxx进行 URLEncoder。如:

public class UrlencoderDemo {

    public static void main(String[] args) throws Exception {
        String url = "https://www.baidu.com?platform=jiaobuchong&name=jack";
        System.out.println(getEncodeQueryUrl(url));
        System.out.println(getEncodeQueryUrl("https://www.baidu.com?"));
        System.out.println(getEncodeQueryUrl("https://www.baidu.com"));
        System.out.println(getEncodeQueryUrl("https://www.baidu.com/"));

    }

    public static String getEncodeQueryUrl(String url) throws UnsupportedEncodingException {

        if (StringUtils.isBlank(url)) {
            throw new IllegalArgumentException("url is blank");
        }
        int pos = url.lastIndexOf("?");
        if (pos > -1 && (pos < (url.length() - 1))) {
            return url.substring(0, pos + 1) + URLEncoder.encode(url.substring(pos + 1), "UTF-8");
        }

        return url;
    }
}

输出:

https://www.baidu.com?platform%3Djiaobuchong%26name%3Djack
https://www.baidu.com?
https://www.baidu.com
https://www.baidu.com/

授权链接变成这样就 ok 了:

https://developers.e.qq.com/oauth/authorize?client_id=123456&redirect_uri=https://www.baidu.com?platform%3Djiaobuchong%26name%3Djack&state=&scope=ADS_MANAGEMENT

参考:
微信授权回调时,回调地址中原有参数丢失
微信开发–授权接口url多参数请求问题
网址URL中特殊字符转义编码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值