当前位置: 移动技术网 > 科技>操作系统>Linux > 微信授权错误:"errcode":40163,"errmsg":"codebeenused

微信授权错误:"errcode":40163,"errmsg":"codebeenused

2019年05月22日  | 移动技术网科技  | 我要评论

微信网页授权获取code值回调两次的问题

1.说是域名原因,目前未测试,没有正确的域名

  1. 问题描述:在调用微信网页授权获取openid值时,先获取的code值,但是code值的接口 会走两次回调。而code在6分钟内只能用一次,所以处出现code失效的问题,问题显示错误码:{‘errcode’:40029,’errmsg’:’invalid code, hints: [ req_id: 0407ns44 ]’}
  2. 解决办法: 出现这个问题是因为域名的问题,本人先使用的花生壳的内网穿透,但是花生壳的免费域名应用的是第三方代理域名,所以在向微信服务器发送请求的时候,微信回调时,会认为你的域名请求不一致,会回调两次,重定向你的服务器两次,只需更改正式域名即可。就会回调一次。(网上说的返回值结束二次回调,和301重定向 都是坑人的,折腾一天还是域名问题

2.说需要一个参数 &connect_redirect=1,这个是解决40029的错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//实际使用生成url的代码 <br>string urluserinfo = oauthapi.getauthorizeurl(appid,
                "http://2a20h48668.imwork.net/weixin/userinfocallback?returnurl=" + returnurl.urlencode(),
                state, oauthscope.snsapi_userinfo);
     // 摘要:
        //     获取验证地址的api,以及参数说明
        //
        // 参数:
        //   appid:
        //     公众号的唯一标识
        //
        //   redirecturl:
        //     授权后重定向的回调链接地址,请使用urlencode对链接进行处理
        //
        //   state:
        //     重定向后会带上state参数,开发者可以填写a-za-z0-9的参数值,最多128字节
        //
        //   scope:
        //     应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
        //
        //   responsetype:
        //     返回类型,请填写code(或保留默认)
        //
        //   addconnectredirect:
        //     加上后可以解决40029-invalid code的问题(测试中)
        public static string getauthorizeurl(string appid, string redirecturl, string state, oauthscope scope, string responsetype = "code"bool addconnectredirect = true);

  最终网址结果

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd84d9cb4875236c9&redirect_uri=http%3a%2f%2f2a20h48668.imwork.net%2fweixin%2fuserinfocallback%3freturnurl%3d%252fweixinjssdk%252findex&response_type=code&scope=snsapi_userinfo&state=jeffreysu-954&connect_redirect=1#wechat_redirect

3.访问的地址是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// <summary>
       /// oauthscope.snsapi_userinfo方式回调
       /// </summary>
       /// <param name="code"></param>
       /// <param name="state"></param>
       /// <param name="returnurl">用户最初尝试进入的页面</param>
       /// <returns></returns>
       public actionresult userinfocallback(string code, string state, string returnurl)
       {
           if (string.isnullorempty(code))
           {
               return content("您拒绝了授权!");
           }
           var orginstate = data.getstate();
 
           if (state != orginstate)
           {
               //这里的state其实是会暴露给客户端的,验证能力很弱,这里只是演示一下,
               //建议用完之后就清空,将其一次性使用
               //实际上可以存任何想传递的数据,比如用户id,并且需要结合例如下面的session["oauthaccesstoken"]进行验证
               return content("验证失败!请从正规途径进入!");
           }
 
           oauthaccesstokenresult result = null;
 
           //通过,用code换取access_token
           try
           {
               result = oauthapi.getaccesstoken(appid, appsecret, code);
           }
           catch (exception ex)
           {
               return content(ex.message);
           }
           if (result.errcode != returncode.请求成功)
           {
               return content("错误:" + result.errmsg);
           }
           //下面2个数据也可以自己封装成一个类,储存在数据库中(建议结合缓存)
           //如果可以确保安全,可以将access_token存入用户的cookie中,每一个人的access_token是不一样的
           httpcontext.session.setstring("oauthaccesstokenstarttime", datetime.now.tostring());
           httpcontext.session.setstring("oauthaccesstoken", result.tojson());
 
           //因为第一步选择的是oauthscope.snsapi_userinfo,这里可以进一步获取用户详细信息
           try
           {
               if (!string.isnullorempty(returnurl))
               {
                   return redirect(returnurl);
               }
 
               oauthuserinfo userinfo = oauthapi.getuserinfo(result.access_token, result.openid);
               return view(userinfo);
           }
           catch (errorjsonresultexception ex)
           {
               return content(ex.message);
           }
       }

  //建议将result存入数据库中,确保值访问一次

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网