服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java微信公众号开发之通过微信公众号获取用户信息

Java微信公众号开发之通过微信公众号获取用户信息

2020-10-06 17:21南=子 Java教程

这篇文章主要介绍了Java微信公众号开发之通过微信公众号获取用户信息,需要的朋友可以参考下

最近由于公司业务,就开始研究微信开发的流程,说实话,这东西刚开始看到时候和看天书的一样,总算,看了一天的文档,测试代码终于出来了。

1、首先需要到微信网站去设置一下,我是直接用的微信测试号。

        接口配置信息必须要填写的,所以说必须能将自己的服务发布出去

Java微信公众号开发之通过微信公众号获取用户信息

          Java微信公众号开发之通过微信公众号获取用户信息

            Java微信公众号开发之通过微信公众号获取用户信息

            Java微信公众号开发之通过微信公众号获取用户信息

                             到此微信配置完毕,接下来就是直接上代码了

2、获取用户信息的方式一共是两种,前提都是用户关注微信公众号,一种是静默获取(snsapi_base,这种方式只能获取openid),另一种是授权获取(snsapi_userinfo,可以获取用户的详细信息)。

      先说第一种

  (1)首先需要先访问微信的链接

    

           这里的 uri就是直接回掉我们的服务地址,一定要记住,服务校验的判断,我是按照来判断的echostr(第二种方式也是这样)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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);
    }
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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微信公众号开发之通过微信公众号获取用户信息,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/yangming5423/archive/2017/05/18/6873267.html

延伸 · 阅读

精彩推荐