虽然ASP已经不再是主流了,但是因为一些历史项目的原因,客户又不愿意舍弃ASP,所以在客户的强烈要求下,要开发微信下的应用,所以写了这个代码,分享给大家。
首先你创建一个页面,这个页面是用户的入口,比如叫 weixin.asp
<script language="javascript">
var url =
"https://open.weixin.qq.com/connect/oauth2/authorize?appid=替换成你的微信APPID&redirect_uri=替换你的返回URL&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
window.location.href=url;
</script>
另外再创建一个页面作为redirect_uri,用来接收微信的返回数据:
<script language="javascript" runat="server"> function evalJsonStr(str){return eval("(" + str + ")");} </script> <% '获取code inf = request.ServerVariables ("QUERY_STRING") inf1 = Split(inf,"code=") inf2 = Split(inf1(1),"&") code = inf2(0) '获取json函数 function jsonObjByUrl(url,method,data) Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP") objXML.open method,url,false objXML.send(data) set jsonObjByUrl=evalJsonStr(objXML.responseText) set objXML=nothing end Function '获取openid Appid="" AppSecret="" url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="&Appid&"&secret="&AppSecret&"&code="&code&"&grant_type=authorization_code" method="GET":dataStr="" set obj=jsonObjByUrl(url,method,dataStr) OPENID=obj.openid '获取userinfo信息 url1="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="&Appid&"&secret="&AppSecret set obj1=jsonObjByUrl(url1,method,dataStr) ACCESS_TOKEN=obj1.access_token url2="https://api.weixin.qq.com/cgi-bin/user/info?access_token="&ACCESS_TOKEN&"&openid="&OPENID&"&lang=zh_CN" set obj2=jsonObjByUrl(url2,method,dataStr) Subscribe=obj2.subscribe '写入session session("openid") = OPENID Response.write session("openid") Response.Cookies("subscribe") = Subscribe %>