java微信开发-之如何获取openid 和用户信息


首先我们要先看一下文档 https://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html


重点的话就是这里的授权页面的参数,文档的话希望自己去看和理解,比较这个方面的话,前辈已经写了很多了,我只是想把我自己的写出来提供参考。


  java微信开发-之如何获取openid 和用户信息_第1张图片

首先肯定的配置微信公众号里面的内容,这里就不多说了也就是域名,js域名,token,这方面的东西,都好之后我这里是从菜单说去,应该挺详细的。

创建微信的一二级菜单,具体的话可能要自己去找资料了。这里只有一级菜单。

接下来我就来解释一下就是通过url得到code,通过code得到openid 然后得到用户信息这样一个思路.


当我们点击,每个人都有的按钮 通过url 后面的域名   redirect_uri=http://www.vicp.com/task/technician/check 

进入我们的 task/technician/check  这个方法传一个code值过去,同时参数appid是填写自己公众号的,其它的基本可以默认,就行.

//授权页面

buttonBuys.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?"+ "appid="+appid+"&redirect_uri="
+ "http://www.vicp.com/task/technician/check&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");


/**
 * 创建自定义菜单
 */
	public static void createMenu() throws Exception {
		String appid = WxConfig.APPID;
		
		JSONObject buttonBuys = new JSONObject();
		buttonBuys.put("type", "view");
		buttonBuys.put("name", "每个人都有");
		//授权页面
		buttonBuys.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?"
		+ "appid="+appid+"&redirect_uri="
		+ "http://www.vicp.com/task/technician/check&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
		
       	// menuKnow 一级菜单
		JSONObject menuKnow = new JSONObject();
		menuKnow.put("type", "view");
		menuKnow.put("name", "我要发布任务");
		//授权页面
		menuKnow.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?"
		+ "appid="+appid+"&redirect_uri="
		+ "http://www.vicp.com/task/employer/check&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
		JSONArray buttonsAll = new JSONArray();
		buttonsAll.add(buttonBuys);
		buttonsAll.add(menuKnow);
		
		JSONObject menu = new JSONObject();
		menu.put("button", buttonsAll);
		menu.toString();
		
		MpApi.menuCreate(menu.toString());
		MpApi.menuQuery();
	}


MpApi.menuCreate(menu.toString()); //这里是调用微信的创建接口 需要自己 post 或者 get 到微信就行了
private static final String CreateMenuUrl =https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

MpApi.menuQuery(); //这里是调用微信的查询接口
需要自己 post 或者 get 到微信就行了
private static final String QueryMenuUrl =https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN

MpApi.GetPageAccessTokenUrl //这里接口在下面 
去掉 mpapi 调用接口就行了

如上说的,我们通过url进入了我们方法得到code,接下来就是得到code了,如果url通过了我们可以直接得到code值


/**
	 * 技术人员任务判断
	 * 
	 * @param session
	 * @param model
	 * @param code
	 * @return
	 */
	@RequestMapping("/technician/check")
	public String check(HttpSession session, Map model,) {
		String openId = null;
		String code = request.getParameter("code");//我们要的code
		try {
			openId = getTheCode(session, code, openId);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	private String getTheCode(HttpSession session, String code, String openId) {
		if (code != null) 
		{
		openId = userService.getOpenid(code);// 调用根据用户的code得到信息
		}
		// 获取基础刷新的接口访问凭证
		AccessToken accessToken = AccessTokenUtil.queryAccessToken();
		User userinfo = userinof.getUserInfo(accessToken.getAccess_token(),openId);
		System.out.println("昵称:" + userinfo.getNickname());

		if (userinfo.getOpenid() == null) 
		{
			// 得到保存在session的微信信息
			User loginUser = BaseUserController.getLoginUser(session);
			openId = loginUser.getOpenid();
			System.out.println(openId + "你是");
		} 
		
		else 
		{
			addSessionUser(session, userinfo);
		}

		return openId;
	}



getOpenid 方法

/**
	 * 识别得到用户id必须的一个值
	 * 
	 * @param code
	 * @return
	 */
	// 根据用户的code得到用户OpenId
	public String getOpenid(String code) {
		UserinofService weixinGetCode = new UserinofService();
		Map result = weixinGetCode.oauth2GetOpenid(code);
		String OpenId = (String) result.get("Openid");// 得到用户id
		
		return OpenId;
	}



UserinofServicele类


package xyz.ibenben.parttime.common.weixxintool;

import net.sf.json.JSONObject;
import xyz.ibenben.parttime.common.weixinapi.HttpUtil;
import xyz.ibenben.parttime.common.weixinapi.MpApi;
import xyz.ibenben.parttime.user.entity.User;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 扫描就获取用户信息
 * 
 * @param accessToken
 *            接口访问凭证
 * @param openId
 *            用户标识
 * @return WeixinUserInfo
 */
@Service
public class UserinofService {
	
	
	/**
	 * 得到微信用户信息
	 * @param accessToken
	 * @param openId
	 * @return
	 */
	public  User getUserInfo(String accessToken, String openId) {
		
		Logger log = LoggerFactory.getLogger(HttpUtil.class);
		User weixinUserInfo = null;
		// 拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID";
		requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace(
				"OPENID", openId);
		// 获取用户信息
		JSONObject jsonObject = HttpUtil
				.httpsRequest(requestUrl, "GET", null);
	
		if (null != jsonObject) {
			try {
				weixinUserInfo = new User();
				// 用户的标识
				weixinUserInfo.setOpenid(jsonObject.getString("openid"));
				// 昵称
				weixinUserInfo.setNickname(jsonObject.getString("nickname"));
				// 用户的性别(1是男性,2是女性,0是未知)
				weixinUserInfo.setSex(jsonObject.getInt("sex"));
				// 用户所在国家
				weixinUserInfo.setCountry(jsonObject.getString("country"));
				// 用户所在省份
				weixinUserInfo.setProvince(jsonObject.getString("province"));
				// 用户所在城市
				weixinUserInfo.setCity(jsonObject.getString("city"));
				// 用户头像
				weixinUserInfo.setHeadImage(jsonObject.getString("headimgurl"));
			} catch (Exception e) {
				if (0 == weixinUserInfo.getSubscribe())
					log.error("用户{}已取消关注", weixinUserInfo.getOpenid());
				else {
					int errorCode = jsonObject.getInt("errcode");
					String errorMsg = jsonObject.getString("errmsg");
					log.error("获取用户信息失败 errcode:{} errmsg:{}", errorCode,
							errorMsg);
				}
			}
		}
		return weixinUserInfo;
	}
	
	/**
	 * 
	 * @param code 识别得到用户id必须的一个值
	 * 得到网页授权凭证和用户id
	 * @return
	 */
	public static Map oauth2GetOpenid(String code) {
		String appid = WxConfig.APPID;//自己的配置appid
		String appsecret = WxConfig.APPSECRET;//自己的配置APPSECRET;
		String requestUrl = MpApi.GetPageAccessTokenUrl.replace("APPID", appid).replace("SECRET", appsecret).replace("CODE", code);
        HttpClient client = null;
        Map result =new HashMap();
        try {	
            client = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(requestUrl);
            ResponseHandler responseHandler = new BasicResponseHandler();
            String response = client.execute(httpget, responseHandler);
            JSONObject OpenidJSONO=JSONObject.fromObject(response);
            
            //OpenidJSONO可以得到的内容:access_token expires_in  refresh_token openid scope 
            
            String Openid =String.valueOf(OpenidJSONO.get("openid"));
            String AccessToken=String.valueOf(OpenidJSONO.get("access_token"));
            String Scope=String.valueOf(OpenidJSONO.get("scope"));//用户保存的作用域
            String refresh_token=String.valueOf(OpenidJSONO.get("refresh_token"));
            
            result.put("Openid", Openid);
            result.put("AccessToken", AccessToken);
            result.put("scope",Scope);
            result.put("refresh_token", refresh_token);
        } catch (Exception e) {
            e.printStackTrace(); 
        } finally {
            client.getConnectionManager().shutdown();
        }
        return result;
    }
// 网页授权获取code
public final static String GetPageCode = "https://open.weixin.qq.com/connect/oauth2/authorize?
appid=APPID&redirect_uri=URL&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
	
// 网页授权接口
public final static String GetPageAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?
appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";

// 网页授权得到用户基本信息接口
public final static String GetPageUsersUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";	
}



上面就是根据code得到openid,现在就是根据openid得到用户信息了


user类

/**
 * 类名: WeixinUserInfo 
* 描述: 微信用户的基本信息
* 开发人员: zdl
* 创建时间: 2015-11-27
发布版本:V1.0
*/ public class Userof { // 用户的标识 private String openId; // 关注状态(1是关注,0是未关注),未关注时获取不到其余信息 private int subscribe; // 用户关注时间,为时间戳。如果用户曾多次关注,则取最后关注时间 private String subscribeTime; // 昵称 private String nickname; // 用户的性别(1是男性,2是女性,0是未知) private int sex; // 用户所在国家 private String country; // 用户所在省份 private String province; // 用户所在城市 private String city; // 用户的语言,简体中文为zh_CN private String language; // 用户头像 private String headImgUrl; public String getOpenId() { return openId; } public void setOpenId(String openId) { this.openId = openId; } public int getSubscribe() { return subscribe; } public void setSubscribe(int subscribe) { this.subscribe = subscribe; } public String getSubscribeTime() { return subscribeTime; } public void setSubscribeTime(String subscribeTime) { this.subscribeTime = subscribeTime; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } public String getHeadImgUrl() { return headImgUrl; } public void setHeadImgUrl(String headImgUrl) { this.headImgUrl = headImgUrl; } }


// 获取用户信息 的url请求

JSONObject jsonObject = HttpUtil.httpsRequest(requestUrl, "GET", null);


// 获取用户信息 的url请求
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;


import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;


import net.sf.json.JSONObject;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 服务器端http请求工具类
 */
public class HttpUtil {
	private static Logger log = LoggerFactory.getLogger(HttpUtil.class);


	/**
	 * utf-8编码
	 */
	static class Utf8ResponseHandler implements ResponseHandler {
		public String handleResponse(final HttpResponse response)
				throws HttpResponseException, IOException {
			final StatusLine statusLine = response.getStatusLine();
			final HttpEntity entity = response.getEntity();
			if (statusLine.getStatusCode() >= 300) {
				EntityUtils.consume(entity);
				throw new HttpResponseException(statusLine.getStatusCode(),
						statusLine.getReasonPhrase());
			}
			return entity == null ? null : EntityUtils
					.toString(entity, "UTF-8");
		}


	}


	/**
	 * 获取用户信息发送 --HTTPS请求
	 * 
	 * @param requestUrl
	 *            请求地址
	 * @param requestMethod
	 *            请求方式(GET、POST)
	 * @param outputStr
	 *            提交的数据
	 * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
	 */
	public static JSONObject httpsRequest(String requestUrl,
			String requestMethod, String outputStr) {
		JSONObject jsonObject = null;
		try {
			// 创建SSLContext对象,并使用我们指定的信任管理器初始化
			TrustManager[] tm = { new MyX509TrustManager() };
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
			sslContext.init(null, tm, new java.security.SecureRandom());
			// 从上述SSLContext对象中得到SSLSocketFactory对象
			SSLSocketFactory ssf = sslContext.getSocketFactory();
			URL url = new URL(requestUrl);
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
			conn.setSSLSocketFactory(ssf);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			// 设置请求方式(GET/POST)
			conn.setRequestMethod(requestMethod);
			// 当outputStr不为null时向输出流写数据
			if (null != outputStr) {
				OutputStream outputStream = conn.getOutputStream();
				// 注意编码格式
				outputStream.write(outputStr.getBytes("UTF-8"));
				outputStream.close();
			}
			// 从输入流读取返回内容
			InputStream inputStream = conn.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(
					inputStream, "utf-8");
			BufferedReader bufferedReader = new BufferedReader(
					inputStreamReader);
			String str = null;
			StringBuffer buffer = new StringBuffer();
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			// 释放资源
			bufferedReader.close();
			inputStreamReader.close();
			inputStream.close();
			inputStream = null;
			conn.disconnect();
			jsonObject = JSONObject.fromObject(buffer.toString());
		} catch (ConnectException ce) {
			log.error("连接超时:{}", ce);
		} catch (Exception e) {
			log.error("https请求异常:{}", e);
		}
		return jsonObject;
	}
}
package com.web.controller.io;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.X509TrustManager;

/**
 * 类名: MyX509TrustManager 
* 描述:信任管理器
* 开发人员: ZDL
* 创建时间:2016-9-27
* 发布版本:V1.0
*/ public class MyX509TrustManager implements X509TrustManager { // 检查客户端证书 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } // 检查服务器端证书 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } // 返回受信任的X509证书数组 public X509Certificate[] getAcceptedIssuers() { return null; } }


如果上面有什么问题欢迎大家加入群 93472007

你可能感兴趣的:(java微信)