以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  获取"openid"授权出错  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=129016)

--  作者:xluoping
--  发布时间:2018/12/19 23:41:00
--  获取"openid"授权出错
微信返回错误:{ "errcode": 40163, "errmsg": "code been used, hints: [ req_id: 99_hNA05062271 ]" }

是哪里出错了?

代码如下:
 If e.GetValues.ContainsKey("code") Then \'如果是通过授权链接跳转而来,就从链接重提取code来获取openid
        Dim ul As String  = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code"
        ul = CExp(ul,"wxac4b441513cd5fa1","21ecf2559557a8205a408f78222d57db",e.GetValues("code"))
        Dim hc As new HttpClient(ul)
        Dim jo As JObject = JObject.Parse(hc.GetData)
        If jo("openid") IsNot Nothing Then \'如果获取openid成功(成功的话,还会同时返回一个accesstiken,用于获取用户详情)
            OpenID = jo("openid")
            Dim dr As DataRow = DataTables("WXUsers").SQLFind("openid = \'" & OpenID & "\'")
            ul = "https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN "
            \'根据openid和accesstoken获取用户详情,注意这里这个accesstoken不是普通accesston,只能用于网页授权
            hc = New HttpClient(CExp(ul, jo("access_token"), OpenId))
            jo = jo.Parse(hc.GetData)
            If jo("openid") IsNot Nothing Then
                UserName = jo("nickname")
                If dr Is Nothing Then
                    dr = DataTables("WXUsers").AddNew()
                    If e.Cookies.ContainsKey("open") AndAlso jo("openid") <> e.Cookies("open") Then  \'如果是分享的
                        dr("上级openid") = e.Cookies("open")
                    End If
                End If
                Dim nms() As String = {"openid","nickname","sex","city","country","province","headimgurl","groupid","remark","language","tagid_list","permit","subscribe_time"} \'""
                For Each nm As String In nms
                    If jo(nm) IsNot Nothing Then
                        dr(nm) = jo(nm)
                    End If
                Next
                dr("全部") = jo.ToString
                dr("permit") = True
                dr.Save
            Else
                e.WriteString(jo.ToString) \'在用户浏览器显示错误信息
                Return ""
            End If
            \' End If
            e.AppendCookie("nickname",UserName) \'用户名和openid存储在Cookie中
            e.AppendCookie("openid",OpenID)
        Else
            e.WriteString(jo.ToString) \'在用户浏览器显示错误信息                  返回这里
            Return ""
        End If


--  作者:xluoping
--  发布时间:2018/12/20 0:18:00
--  
搞错了
--  作者:有点甜
--  发布时间:2018/12/20 9:13:00
--  

看代码没有问题,请贴出完整的代码,加入msgbox弹出值看看是否正确。

 

http://www.foxtable.com/mobilehelp/scr/0236.htm