前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信OAuth2.0网页授权多回调域名

微信OAuth2.0网页授权多回调域名

作者头像
似水的流年
发布2019-12-08 20:28:19
3.5K0
发布2019-12-08 20:28:19
举报
文章被收录于专栏:电光石火电光石火

转载自 https://github.com/HADB/GetWeixinCode

使用方法

  1. 部署 get-weixin-code.html 至你的微信授权回调域名的目录下
  2. 使用方式类似于直接通过微信回调的方式,只是将回调地址改成了 get-weixin-code.html 所在的地址,另外省去了response_type 参数(因为它只能为code )以及#wechat_redirect (它是固定的),它们会在get-weixin-code.html 里面自己加上
  3. get-weixin-code.html 页面从微信那里拿到code之后会重新跳转回redirect_uri 里面填写的url,并且在url后面带上code 和state

详细示例

  1. 前往微信公众平台->接口权限->网页授权获取用户基本信息->修改,填写授权回调页面域名,例如 www.abc.com
  2. 在 www.abc.com 域名下部署get-weixin-code.html ,不一定是根目录,例如:http://www.abc.com/xxx/get-weixin-code.html
  3. 假设你的 http://www.xyz.com/hello-world.html 这个页面需要获取微信授权,那么你应该使用以下地址来获取授权:http://www.abc.com/xxx/get-weixin-code.html?appid=XXXX&scope=snsapi_base&state=hello-world&redirect_uri=http%3A%2F%2Fwww.xyz.com%2Fhello-world.html
  4. 这样最终就会跳转到这样一个地址: http://www.xyz.com/hello-world.html?code=XXXXXXXXXXXXXXXXX&state=hello-world ,从而你就拿到了授权code 以及自定义的state 参数了
代码语言:javascript
复制
<!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;
                }
            };

            GWC.getUrlParams();
            GWC.doRedirect();
        </script>
    </body>

</html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-01-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用方法
  • 详细示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档