Foxtable(狐表)用户栏目专家坐堂 → 公众号授权获取openid和name失败


  共有98人关注过本帖树形打印复制链接

主题:公众号授权获取openid和name失败

帅哥哟,离线,有人找我吗?
YDS
  1楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:606 积分:3403 威望:0 精华:0 注册:2011/5/21 18:05:00
公众号授权获取openid和name失败  发帖心情 Post By:2024/5/9 8:53:00 [只看该作者]

以下代码是内部函数GetOpenID的全部代码,我再网页调用该函数无法获取iopenid和name,请教一下是哪里出了问题?

Dim e As RequestEventArgs = args(0)
Dim sb As New StringBuilder
sb.AppendLine("<meta name='viewport' c>")
If e.host = Functions.Execute("Get", "公众号", "授权域名") Then '授权测试
    Dim UserName As String
    Dim OpenID As String
    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, Functions.Execute("Get", "公众号", "AppID"), Functions.Execute("Get", "公众号", "AppSecret"), 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").Find("openid ='" & Openid & "'")
            If dr IsNot Nothing Then
                UserName = dr("nickname")
            Else
                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")
                    dr = DataTables("WXUsers").AddNew()
                    Dim nms() As String = {"openid", "nickname", "sex", "city", "country", "province", "headimgurl"} '""
                    For Each nm As String In nms
                        dr(nm) = jo(nm)
                    Next
                    dr.Save
                Else
                    e.WriteString(jo.ToString) '在用户浏览器显示错误信息
                    Return Nothing
                End If
            End If
            e.AppendCookie("username", UserName) '用户名和openid存储在Cookie中
            e.AppendCookie("openid", OpenID)
            Return OpenID & "|" & UserName
        Else
            e.WriteString(jo.ToString) '在用户浏览器显示错误信息
            Return Nothing
        End If
    Else
        UserName = e.Cookies("username") '从cookie获取用户名和openid
        OpenID = e.Cookies("openid")
        If userName = "" OrElse OpenID = "" Then
            Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"
            ul = CExp(ul, Functions.Execute("Get", "公众号", "AppID"), UrlEncode(Functions.Execute("Get", "公众号", "授权域名")))
            sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
            e.WriteString(sb.ToString)
            Return Nothing
        End If
    End If
End If

 回到顶部
帅哥,在线噢!
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:106690 积分:542645 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/5/9 9:00:00 [只看该作者]

调试


Dim e As RequestEventArgs = args(0)
Dim sb As New StringBuilder
sb.AppendLine("<meta name='viewport' c>")
If e.host = Functions.Execute("Get", "公众号", "授权域名") Then '授权测试
MsgBox(1)
    Dim UserName As String
    Dim OpenID As String
    If e.GetValues.ContainsKey("code") Then '如果是通过授权链接跳转而来,就从链接重提取code来获取openid
 MsgBox(21) 
        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, Functions.Execute("Get", "公众号", "AppID"), Functions.Execute("Get", "公众号", "AppSecret"), e.GetValues("code"))
        Dim hc As New HttpClient(ul)
        Dim jo As JObject = JObject.Parse(hc.GetData)
         MsgBox(jo.tostring) 
……
    Else
         MsgBox(22) 
        UserName = e.Cookies("username") '从cookie获取用户名和openid
        OpenID = e.Cookies("openid")
        If userName = "" OrElse OpenID = "" Then
            MsgBox(221) 
            Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"
            ul = CExp(ul, Functions.Execute("Get", "公众号", "AppID"), UrlEncode(Functions.Execute("Get", "公众号", "授权域名")))
            MsgBox(ul) 
            sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
            e.WriteString(sb.ToString)
            Return Nothing
        End If
    End If
End If

 回到顶部
帅哥哟,离线,有人找我吗?
YDS
  3楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:606 积分:3403 威望:0 精华:0 注册:2011/5/21 18:05:00
  发帖心情 Post By:2024/5/9 9:20:00 [只看该作者]

已经找到问题,谢谢!但是无法获取userName,这个是什么问题?


 回到顶部
帅哥,在线噢!
有点蓝
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:106690 积分:542645 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/5/9 9:23:00 [只看该作者]


 回到顶部