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

作者:南=子 时间:2021-12-16 08:19:19 

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

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

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

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

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

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

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

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

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

      先说第一种

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

https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxxxxxxxxxx&redirect_uri=http://xxxxxx/open/openid&response_type=code&scope=snsapi_base

           这里的 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)

https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxx&redirect_uri=http:// 域名

/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微信公众号开发之通过微信公众号获取用户信息网站的支持!

来源:http://www.cnblogs.com/yangming5423/archive/2017/05/18/6873267.html

标签:java,微信公众号,用户信息
0
投稿

猜你喜欢

  • Spring JDBCTemplate原理及使用实例

    2023-03-11 09:47:19
  • SpringMVC利用dropzone组件实现图片上传

    2022-01-19 00:37:00
  • Java maven三种仓库,本地仓库,私服,中央仓库的配置

    2023-04-13 12:05:17
  • 全面解析java final关键字

    2023-03-16 03:04:55
  • SSM框架+Plupload实现分块上传大文件示例

    2023-06-01 01:58:21
  • 基于WPF实现面包屑控件的示例代码

    2021-12-19 12:34:33
  • Struts2相关的面试题整理分享

    2022-04-06 08:02:20
  • 使用JPA自定义VO类型转换(EntityUtils工具类)

    2023-08-26 14:56:17
  • 如何使用JCTools实现Java并发程序

    2023-11-21 07:37:39
  • C#中Linq延迟查询的例子

    2022-01-15 02:43:11
  • 一篇文章带你复习java知识点

    2023-01-15 09:39:38
  • Java模拟qq软件的详细过程

    2022-01-27 15:06:19
  • java构造函数示例(构造方法)

    2022-05-08 19:06:03
  • JavaSE实现猜拳游戏

    2023-12-22 07:21:19
  • java项目构建Gradle的使用教程

    2023-06-07 19:17:41
  • 详解Java中while和do-while循环、break的使用

    2022-10-24 13:37:04
  • C#文件流读写和进度回调示例详解

    2022-08-12 14:52:31
  • mybatis-plus 使用Condition拼接Sql语句各方法的用法

    2022-07-06 03:51:12
  • JAVA IDEA 打开assert 设置方式

    2022-08-19 13:48:49
  • c#中datagridview处理非绑定列的方法

    2023-06-15 16:52:31
  • asp之家 软件编程 m.aspxhome.com