asp获取微信公众号用户的头像和微信名及openid源码,asp写的,可以获取用户的头像和微信名,性别及openid等
<script language="jscript" runat="server">
function getjson(str){
try{
eval("var jsonStr = (" + str + ")");
}catch(ex){
var jsonStr = null;
}
return jsonStr;
}
</script>
<%
‘==================================
‘=类 名 称:微信登录
‘=功 能:微信登录 For ASP
‘=作 者:18611436777
‘=微信:18611436777
‘=日 期:2014-3
‘==================================
‘转载时请保留以上内容!!
Class WxAPI
Private APPID
Private APPSECRET
Private CALLBACK_URL
Private SCOPE
Private Sub Class_Initialize
APPID = "wxe4a757eaba87d647"‘APP ID
APPSECRET = "9a2f4790e1fe303dc500bf63bf5bad51"‘APP KEY
CALLBACK_URL = "http://www.muyunet.com/weixin/user2.asp"‘REDIRECT_URI
SCOPE ="snsapi_userinfo" ‘授权项snsapi_userinfo,snsapi_base
End Sub
‘Microsoft.XMLHTTP
‘Get方法请求url,获取请求内容
Private Function RequestUrl(url)
Set XmlObj = Server.CreateObject("Microsoft.XMLHTTP")
XmlObj.open "GET",url, false
XmlObj.send
If XmlObj.Readystate=4 Then
RequestUrl = XmlObj.responseText
Else
Response.Write("xmlhttp请求超时!")
Response.End()
End If
Set XmlObj = nothing
End Function
Private Function CheckData(data,str)
If Instr(data,str)>0 Then
CheckData = True
Else
CheckData = False
End If
End Function
‘生成登录地址
Public Function GetAuthorization_Code()
Dim url, params
url = "https://open.weixin.qq.com/connect/oauth2/authorize"
params = "appid="&APPID
params = params & "&redirect_uri=" & server.URLEncode(CALLBACK_URL)
params = params & "&response_type=code"
params = params & "&scope="&SCOPE
params = params & "&state=123#wechat_redirect"
url = url & "?" & params
GetAuthorization_Code = (url)
End Function
‘获取 access_token
Public Function GetAccess_Token(sCode)
Dim url, params,Temp
Url="https://api.weixin.qq.com/sns/oauth2/access_token"
params = "appid="&APPID
params = params & "&secret=" & APPSECRET
params = params & "&code=" & sCode
params = params & "&grant_type=authorization_code"
url = Url & "?" & params
Temp=RequestUrl(url)
If CheckData(Temp,"access_token") = True Then
set obj = getjson(Temp)
if isobject(obj) Then
GetAccess_Token = Array(obj.access_token,obj.openid)
End If
set obj = Nothing
Else
Response.Write("获取 access_token 时发生错误,错误信息:"&Temp)
Response.End()
End If
End Function
‘检测是否合法登录!
Public Function CheckLogin()
Dim Code,mState
Session("Code")=Trim(Request.QueryString("code"))
If Code<>"" Then
CheckLogin = True
Else
CheckLogin = False
End If
End Function
‘获取用户信息,得到一个json格式的字符串
Public Function GetUserInfo()
Dim url, params, result
url = "https://api.weixin.qq.com/sns/userinfo?"
params = params & "&access_token=" & Access_Token
params = params & "&openid=" & Openid
params = params & "&lang=zh_CN"
url = url & "?" & params
Temp = RequestUrl(url)
If CheckData(Temp,"nickname") = False Then
set obj = getjson(Temp)
if isobject(obj) Then
ret = obj.errcode
msg = obj.errmsg
End If
set obj = Nothing
Response.Write("获取用户信息时发生错误,错误代码:"&ret&" , 错误描述:"&msg)
Response.End()
End If
GetUserInfo = Temp
End Function
‘获取用户名字,性别,从json字符串里截取相关字符
Public Function GetUserName(json)
Dim nickname,sex,headimgurl,obj
set obj = getjson(json)
if isobject(obj) Then
nickname = obj.nickname
sex = obj.sex
headimgurl = obj.headimgurl
End If
set obj = Nothing
GetUserName = Array(nickname,sex,headimgurl)
End Function
End Class
%>
==================
互相学习电话微信:18611436777(加微信注明事由)
原文地址:https://www.cnblogs.com/lytocnc/p/9759976.html