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