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がありません。解決策は、コールバックアドレスのクエリパラメータ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/

承認リンクは次のように問題ありません。

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 issue
urlURLエスケープされたエンコード特殊文字

おすすめ

転載: blog.csdn.net/jiaobuchong/article/details/89344489