Java微信公众号开发之通过微信公众号获取用户信息
程序员文章站
2024-02-23 09:00:28
最近由于公司业务,就开始研究微信开发的流程,说实话,这东西刚开始看到时候和看天书的一样,总算,看了一天的文档,测试代码终于出来了。
1、首先需要到微信网站去设置一下,我是...
最近由于公司业务,就开始研究微信开发的流程,说实话,这东西刚开始看到时候和看天书的一样,总算,看了一天的文档,测试代码终于出来了。
1、首先需要到微信网站去设置一下,我是直接用的微信测试号。
接口配置信息必须要填写的,所以说必须能将自己的服务发布出去
到此微信配置完毕,接下来就是直接上代码了
2、获取用户信息的方式一共是两种,前提都是用户关注微信公众号,一种是静默获取(snsapi_base,这种方式只能获取openid),另一种是授权获取(snsapi_userinfo,可以获取用户的详细信息)。
先说第一种
(1)首先需要先访问微信的链接
这里的 uri就是直接回掉我们的服务地址,一定要记住,服务校验的判断,我是按照来判断的echostr(第二种方式也是这样)
package net.itraf.controller; import java.io.ioexception; import java.io.inputstream; import java.io.printwriter; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import javax.servlet.http.httpservletresponse; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responsebody; import com.alibaba.fastjson.jsonobject; @controller @requestmapping("/open") public class opencontroller { @requestmapping("/toopenid") public @responsebody string getopenid(string code,string echostr,httpservletresponse res) throws ioexception{ if(echostr==null){ string url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx24d47d2080f54c5b&secret=95011ac70909e8cca2786217dd80ee3f&code="+code+"&grant_type=authorization_code"; system.out.println(code); string openid=""; try { url geturl=new url(url); httpurlconnection http=(httpurlconnection)geturl.openconnection(); http.setrequestmethod("get"); http.setrequestproperty("content-type","application/x-www-form-urlencoded"); http.setdooutput(true); http.setdoinput(true); http.connect(); inputstream is = http.getinputstream(); int size = is.available(); byte[] b = new byte[size]; is.read(b); string message = new string(b, "utf-8"); jsonobject json = jsonobject.parseobject(message); openid = json.getstring("openid"); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return openid; }else{ printwriter out = res.getwriter(); out.print(echostr); return null; } } //做服务器校验 @requestmapping("/tovalid") public void valid(string echostr,httpservletresponse res) throws ioexception{ printwriter out = res.getwriter(); out.print(echostr); } }
第二种
(1)
域名
/open/openid&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect
package net.itraf.controller; import java.io.ioexception; import java.io.printwriter; import javax.servlet.servletexception; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import net.sf.json.jsonobject; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; @controller @requestmapping("/weixin") public class oauth2action { @requestmapping("/oauth") public void auth(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string echostr = request.getparameter("echostr"); if(echostr==null){ string appid = "wx24d47d2080f54c5b"; string appsecret = "95011ac70909e8cca2786217dd80ee3f"; //拼接 string get_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + "appid=" + appid + "&secret=" + appsecret + "&code=code&grant_type=authorization_code"; string get_userinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=access_token&openid=openid&lang=zh_cn"; request.setcharacterencoding("utf-8"); response.setcharacterencoding("utf-8"); string code = request.getparameter("code"); system.out.println("******************code=" + code); get_access_token_url = get_access_token_url.replace("code", code); string json = httpsgetutil.dohttpsgetjson(get_access_token_url); jsonobject jsonobject = jsonobject.fromobject(json); string access_token = jsonobject.getstring("access_token"); string openid = jsonobject.getstring("openid"); get_userinfo = get_userinfo.replace("access_token", access_token); get_userinfo = get_userinfo.replace("openid", openid); string userinfojson = httpsgetutil.dohttpsgetjson(get_userinfo); jsonobject userinfojo = jsonobject.fromobject(userinfojson); string user_openid = userinfojo.getstring("openid"); string user_nickname = userinfojo.getstring("nickname"); string user_sex = userinfojo.getstring("sex"); string user_province = userinfojo.getstring("province"); string user_city = userinfojo.getstring("city"); string user_country = userinfojo.getstring("country"); string user_headimgurl = userinfojo.getstring("headimgurl"); response.setcontenttype("text/html; charset=utf-8"); printwriter out = response.getwriter(); out.println("<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\">"); out.println("<html>"); out.println(" <head><title>a servlet</title></head>"); out.println(" <body>"); out.print(" this is "); out.print(this.getclass()); out.println(", using the post method \n"); out.println("openid:" + user_openid + "\n\n"); out.println("nickname:" + user_nickname + "\n\n"); out.println("sex:" + user_sex + "\n\n"); out.println("province:" + user_province + "\n\n"); out.println("city:" + user_city + "\n\n"); out.println("country:" + user_country + "\n\n"); out.println("<img src=/" + user_headimgurl + "/"); out.println(">"); out.println(" </body>"); out.println("</html>"); out.flush(); out.close(); }else{ printwriter out = response.getwriter(); out.print(echostr); } } }
package net.itraf.controller; import java.io.ioexception; import java.io.inputstream; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; public class httpsgetutil { public static string dohttpsgetjson(string url) { string message = ""; try { system.out.println("dohttpsgetjson");//todo:dd url urlget = new url(url); httpurlconnection http = (httpurlconnection) urlget.openconnection(); http.setrequestmethod("get"); //必须是get方式请求 24 http.setrequestproperty("content-type","application/x-www-form-urlencoded"); http.setdooutput(true); http.setdoinput(true); system.setproperty("sun.net.client.defaultconnecttimeout", "30000");//连接超时30秒28 system.setproperty("sun.net.client.defaultreadtimeout", "30000"); //读取超时30秒29 30 http.connect(); inputstream is =http.getinputstream(); int size =is.available(); byte[] jsonbytes =new byte[size]; is.read(jsonbytes); message=new string(jsonbytes,"utf-8"); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return message; } }
以上所述是小编给大家介绍的java微信公众号开发之通过微信公众号获取用户信息,希望对大家有所帮助