asp微信登录获取微信openid、用户名及头像和access_token

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tocnc/article/details/87617972

可以用在微信上做为会员注册来用,不用在使用帐户密码了,代码是用asp独立完成编写的,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
'=日    期: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://ly.ftphn.com/weixin/user2.asp"'这里的地址,必须要以http;//开头
		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
%>
展开阅读全文

没有更多推荐了,返回首页