javascript - WeChat authorized login code 40163
三叔
三叔 2017-06-15 09:22:23
0
2
2948

1. Enter the following page from the public account path
""https://---------/wxCode?appid=--------&redirect_uri=---- ---&response_type=code&scope=snsapi_userinfo

2. The html code of wxCode is the code on github. The source code is https://github.com/HADB/GetWe...

代码如下:
<!DOCTYPE html>
<html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>微信登录</title>
    </head>
    <body>
        <script>
            var GWC = {
                version: '1.1.1',
                urlParams: {},
                appendParams: function(url, params) {
                    if (params) {
                        var baseWithSearch = url.split('#')[0];
                        var hash = url.split('#')[1];
                        for (var key in params) {
                            var attrValue = params[key];
                            if (attrValue !== undefined) {
                                var newParam = key + "=" + attrValue;
                                if (baseWithSearch.indexOf('?') > 0) {
                                    var oldParamReg = new RegExp('^' + key + '=[-%.!~*\'\(\)\w]*', 'g');
                                    if (oldParamReg.test(baseWithSearch)) {
                                        baseWithSearch = baseWithSearch.replace(oldParamReg, newParam);
                                    } else {
                                        baseWithSearch += "&" + newParam;
                                    }
                                } else {
                                    baseWithSearch += "?" + newParam;
                                }
                            }
                        }
                        if (hash) {
                            url = baseWithSearch + '#' + hash;
                        } else {
                            url = baseWithSearch;
                        }
                    }
                    return url;
                },
                getUrlParams: function() {
                    var pairs = location.search.substring(1).split('&');
                    for (var i = 0; i < pairs.length; i++) {
                        var pos = pairs[i].indexOf('=');
                        if (pos === -1) {
                            continue;
                        }
                        GWC.urlParams[pairs[i].substring(0, pos)] = decodeURIComponent(pairs[i].substring(pos + 1));
                    }
                },
                doRedirect: function() {
                    var code = GWC.urlParams['code'];
                    var appId = GWC.urlParams['appid'];
                    var scope = GWC.urlParams['scope'] || 'snsapi_base';
                    var state = GWC.urlParams['state'];
                    var redirectUri;
                    if (!code) {
                        //第一步,没有拿到code,跳转至微信授权页面获取code
                        redirectUri = GWC.appendParams('https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect', {
                            'appid': appId,
                            'redirect_uri': encodeURIComponent(location.href),
                            'response_type': 'code',
                            'scope': scope,
                            'state': state,
                        });
                    } else {
                        //第二步,从微信授权页面跳转回来,已经获取到了code,再次跳转到实际所需页面
                        redirectUri = GWC.appendParams(GWC.urlParams['redirect_uri'], {
                            'code': code,
                            'state': state
                        });
                    }
                    location.href = redirectUri;
                }
            };

        </script>
    </body>
</html>

3. When entering the parameter page, redirect_uri=------, get the code from the url, save it in the cookie, and record it.

4. In this page, a php interface is called. Get the code and pass it over. PHP gets the code and calls the WeChat interface to get the access_token. The interface returns information that is always errcode = 40163; errmsg = code been used. People are almost going to collapse.

Since the above cannot solve the problem, the following adjustments have been made:

1. Make the entry from the Php file and go directly to authorization. The jump page is the H5 html file under the current domain name. After the authorization is passed, the html parameter of the jump page contains code.
2. Request the php interface in this H5 html and pass the code in the url.
3.php obtains the access_token through the passed code, and also reports errcode = 40163; errmsg = code been used

The code is as follows:
Entry path
ht tps://api##/getWXCode? redirect_uri=htt ps://api/Minicustomer/receive (here is a function in the php file)

The rendered page url with code parameters is passed in this page through

$.getJSON("https://api
##*##/WxRedPack?code=" $.isUrlPar("code") "&amount=" $.cookie("amount") " &callback=?", function(data) { The code is passed to the php interface. When calling the WeChat interface to obtain the access_token, it always reports errcode = 40163;errmsg = code been used

崩溃!!!

Therefore, seek the cause and solution.

三叔
三叔

reply all(2)
代言

The code can only be used once. The code obtained in js is used twice when it is transferred to other pages; my suggestion is to operate it from php. After obtaining the code in php, directly obtain the access_token

曾经蜡笔没有小新

GWC.urlParams['redirect_uri'] should represent the specific backend address

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template