微信之关于如何获取用户基本信息

关于如何获取微信已关注用户的基本信息,我这里先说一下步骤:那些基本的信息就不多说了,如AppIDAppSecret1、首先成为开发者,需要外网,如果没有外网,建议在花生壳申请一个账号,通过映射,可以映射到外网,配置好了,如下界面

2、完成第一步后,接下来就是获取opinid了,关于如何获取openID,其实是比较头疼的,网上给出了各种各样的答案,我这里详细说一下,


3、设置好了回调域名,接下来就是通过网页来授权了

文档也说了,有两种方式:

我用的是第二种,因为第一种要用到https协议,工作紧,故没有去试,那我直接说第二种:

请求地址是:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
APPID不用说了吧,redirect_uri这个,请记住,这个一定是你回调域名下面的一级,如果你的回调域名为

那你的redirect_uri填的是weixin下的一级,不能跨级。weixin这个是我的项目名,那redirect_uri可以填weixin/servlet,也可以填weixin/index.jsp,记得URL转码。scopesnsapi_userinfo,其他参数默认就行了,稍后本人会加上我的项目供大家参考,因为本人也很菜,代码就见笑了。。

如果这个填好了,那其他的基本没什么问题了,如果有疑问,欢迎大家加我工作qq320302887,乐意解答。

接下来用代码解释一下。

 这是我们要获得用户的一些基本信息(User类)

//用户的唯一标识
	 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;

记得get set这些属性。

这是我们要获得token,获得的Accesstoken只有两个小时的生命,多刷新。

// 接口访问凭证
	  private String accessToken;
	  // 凭证有效期,单位:秒
	  private int expiresIn;

 记得get set这两个属性。 要这个类
public class MyX509TrustManager implements X509TrustManager{
	// 检查客户端证书
	@Override
	public void checkClientTrusted(X509Certificate[] chain, String authType)
			throws CertificateException {
		// TODO Auto-generated method stub
		
	}
	// 检查服务器端证书
	@Override
	public void checkServerTrusted(X509Certificate[] chain, String authType)
			throws CertificateException {
		// TODO Auto-generated method stub
		
	}
	// 返回受信任的X509证书数组
	@Override
	public X509Certificate[] getAcceptedIssuers() {
		// TODO Auto-generated method stub
		return null;
	}



然后还写一个CommonUtil类,

import java.io.BufferedReader;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CommonUtil {
//	private static Logger log = LoggerFactory.getLogger(CommonUtil.class);
	// 凭证获取(GET)
//	public final static String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=OPENID&secret=APPSECRET";

	/**
	 * 发送https请求
	 * "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID"
	 * 
	 * @param requestUrl
	 *            请求地址
	 * @param requestMethod
	 *            请求方式(GET、POST)
	 * @param outputStr
	 *            提交的数据
	 * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
	 */
	public static JSONObject httpsRequest(String requestUrl,
			String requestMethod, String outputStr) {
            System.out.println("进入httpsRequest方法");
            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);
		}
//		System.out.println("json为123:"+jsonObject);
		return jsonObject;
	}
	/**
	 * 获取接口访问凭证
	 * 
	 * @param appid
	 *            凭证
	 * @param appsecret
	 *            密钥
	 * @return 
	 *         https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential
	 *         &appid=OPENID&secret=APPSECRET
	 */
	public static Token getToken(String appid, String appsecret) {
		Token token = null;
		String requestUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appsecret; 
//		String requestUrl = token_url.replace("APPID", appid).replace(
//				"APPSECRET", appsecret);
		// 发起GET请求获取凭证
		JSONObject jsonObject = httpsRequest(requestUrl, "GET", null);
		if (null != jsonObject) {
			try {
				token = new Token();
				token.setAccessToken(jsonObject.getString("access_token"));
				token.setExpiresIn(jsonObject.getInt("expires_in"));
			} catch (Exception e) {
				token = null;
//				// 获取token失败
//				log.error("获取token失败 errcode:{} errmsg:{}",
//						jsonObject.getInt("errcode"),
//						jsonObject.getString("errmsg"));
			}
		}
		System.out.println(token);
		return token;
	}

	/**
	 * URL编码(utf-8)
	 * 因为只是获取用户基本信息,故下面两个方法用不到
	 * @param source
	 * @return
	 */
	
/*	public static String urlEncodeUTF8(String source) {
		String result = source;
		try {
			result = java.net.URLEncoder.encode(source, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}*/

	/**
	 * 根据内容类型判断文件扩展名
	 * 
	 * @param contentType
	 *            内容类型
	 * @return
	 */
/*	public static String getFileExt(String contentType) {
		String fileExt = "";
		if ("image/jpeg".equals(contentType))
			fileExt = ".jpg";
		else if ("audio/mpeg".equals(contentType))
			fileExt = ".mp3";
		else if ("audio/amr".equals(contentType))
			fileExt = ".amr";
		else if ("video/mp4".equals(contentType))
			fileExt = ".mp4";
		else if ("video/mpeg4".equals(contentType))
			fileExt = ".mp4";
		return fileExt;
	}*/
}

个类就这两个方法供使用。

然后就是用户信息实现的类我这里省去,因为我直接写在servlet里了,额,代码好乱,等下再整理吧,加上自己写代码的过程。

public class Servlet extends HttpServlet {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private static String code;
    private static  String oauth2_url;
    private static String openid;
    private static String appid="wxd00dac2a2ee71120";
    private static String appsecret="db9f234cd02a73e809f5ab02a35e6f09";
    private static JSONObject jsonObject;
    private static JSONObject jsonObject1;
    private static String  accessToken;
    private static User user;
//    private static int subscribe;
    
//    private static String access_token="EMpAts8L2GNBPCqd7JwZSD4vAsS9SjK_MoF1aTGxqe8iRXY4Dgh7xZAhDdVpfTFkG6SC9_SuYzyHMNQopxqFrtZdqE9zNZsrJerPY28-JiRqCooYncA3dg8bH1q3JwslDWZdABARKN";
//    public String Getco(){
//        return code;
//    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        System.out.println("Servlet");
        code=request.getParameter("code");
        oauth2_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd00dac2a2ee71120&secret=db9f234cd02a73e809f5ab02a35e6f09&code="+code+"&grant_type=authorization_code";
        System.out.println("code:" + code);
//        String openId = CommonUtil1.getOpenId(code);
//        System.out.println("openid"+openId);
         jsonObject = CommonUtil.httpsRequest(oauth2_url, "POST", null);
         openid = jsonObject.getString("openid");
        
//         subscribe=jsonObject.getInt("subscribe");
//         System.out.println("获得的subscribe为"+subscribe);
        System.out.println("获得的openID为"+openid);
        Token token = CommonUtil.getToken(appid, appsecret);
        accessToken=token.getAccessToken();
        System.out.println(token);
        System.out.println("最后的opinion*-----"+openid);
         user = getUserInfo(accessToken,openid);
        
         int s=jsonObject1.getInt("subscribe");
        
         System.out.println(s);
        
         System.out.println("------------------------");
//         openid = jsonObject.getString("openid");
         System.out.println(openid);
//         int subscribe = jsonObject.getInt("subscribe");
         System.out.println(jsonObject);
//         System.out.println(subscribe);
        System.out.println("这个user为、、、、、、、、、、"+user);
    }
    public static User getUserInfo(String accessToken, String openId) {
        System.out.println("进入getUserInfo方法");
         user = null;
//         jsonObject1=null;
         String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accessToken+"&openid="+openId;
        //appid wx69d4d8acc08af18e
        //9f4b4d5d514b45054d25c1c2787062cf
        // 拼接请求地址
        //https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID
//        requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace(
//                "OPENID", openId);
        // 获取用户信息
         jsonObject1 = CommonUtil
                .httpsRequest(requestUrl, "POST", null);
         System.out.println(jsonObject1);
         System.out.println("到这了没有");
         if (jsonObject1.getJSONArray("tagid_list").isEmpty()) {
             System.out.println("进来了没有");
//             user.setOpenId(jsonObject1.getString("openid"));
             System.out.println("这里的opinid777777777");
//             int ss = jsonObject1.getInt("subscribe");
//             user.setSubscribe(jsonObject1.getInt("subscribe"));
//             System.out.println("执行了没有");
            return user;
        }
           System.out.println("想要的json:"+jsonObject1);
        if (null != jsonObject1) {
            try {
                user = new User();
                // 用户的标识
                System.out.println(333);
                user.setOpenId(jsonObject1.getString("openid"));
                // 关注状态(1是关注,0是未关注),未关注时获取不到其余信息
//                user.setSubscribe(ss);
                // 用户关注时间
                user.setSubscribeTime(jsonObject1.getString("subscribe_time"));
                // 昵称
                user.setNickname(jsonObject1.getString("nickname"));
                // 用户的性别(1是男性,2是女性,0是未知)
                user.setSex(jsonObject1.getInt("sex"));
                // 用户所在国家
                user.setCountry(jsonObject1.getString("country"));
                // 用户所在省份
                user.setProvince(jsonObject1.getString("province"));
                // 用户所在城市
                user.setCity(jsonObject1.getString("city"));
                // 用户的语言,简体中文为zh_CN
                user.setLanguage(jsonObject1.getString("language"));
                // 用户头像
                user.setHeadImgUrl(jsonObject1.getString("headimgurl"));
            } catch (Exception e) {
//                 if (0 == user.getSubscribe()) {
//                 log.error("用户{}已取消关注", user.getOpenId());
//                 } else {
//                 int errorCode = jsonObject.getInt("errcode");
//                 String errorMsg = jsonObject.getString("errmsg");
//                 log.error("获取用户信息失败 errcode:{} errmsg:{}", errorCode,
//                 errorMsg);
//                 }
                e.printStackTrace();
            }
        }
//        System.out.println("user为:--"+user);
        return user;
    }

这里有一点要注意,如果用户没有关注就无法获得他的基本信息了,只能获得他的openID和关注状态,关注的用户就可以获得全部了

就以上代码放同一个包应该就不会报错了,

以下是微信客户端打开链接获得的信息:


还有一些jar包需要下载


这里要配置一下web.xml,配置那个servlet。。。

所需jar下载地址:http://pan.baidu.com/s/1bo2n4qr 密码:8y3m。



















  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
微信H5网页授权是指在使用微信浏览器访问H5网页时通过微信授权登录,获取用户基本信息。这个过程分为三个步骤:引导用户授权、获取授权码、通过授权码获取用户信息。 首先,用户进入H5网页后,网页需要引导用户进行授权登录。网页可以通过调用微信JS-SDK中的微信授权接口,弹出微信授权登录的窗口。用户点击确认后,微信会生成一个授权码,并跳转回H5网页。 然后,网页需要使用授权码去微信服务器获取用户基本信息。网页可以通过HTTP请求,将授权码发送给微信服务器的接口,并附上AppID和AppSecret等参数。微信服务器验证授权码的有效性后,会返回用户基本信息,如openid、昵称、头像等。 最后,网页可以根据获取用户基本信息,进行相应的业务操作。比如显示用户的头像和昵称,或者根据openid等唯一标识,将用户与其它业务系统进行关联。 需要注意的是,进行微信H5网页授权需要先申请微信开放平台的开发者账号,并创建一个公众号或移动应用。通过在微信开放平台进行配置,获取AppID和AppSecret等必要的参数,用于网页授权的流程中。 总结起来,微信H5网页授权获取用户基本信息是通过使用微信的授权接口,引导用户进行授权登录,再通过授权码和微信服务器进行交互,最终获取用户基本信息。这个过程可以实现在H5网页上使用微信账号登录,并获取用户信息的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的蜗牛905

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值