微信二维码扫码获取openid

扫描二维码后,通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑。

第一步 确定回调域名,即扫描二维码后你需要跳转的后台URL,例如:

http://myWechatTest.top/myProject/wechat_queueInfo.do?user=abc&pwd=123456

http://myWechatTest.top  --> 我的外网域名,类似于localhost:8080 但localhost只有内网才能访问。

myProject --> 我的项目名

第二步 构造URL:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx123456789&redirect_uri=http%3A%2F%2FmyWechatTest.top%2FmyProject%2Fwechat_queueInfo.do%3Fuser%3Dabc%26pwd%3D123456&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect

黑色部分:是腾讯后台的Oauth2.0鉴权接口,是固定写法,appid为微信公众号的appid

红色部分:redirect_url = xxx   ; xxx是第一步中的回调域名,不同的是此处进行了编码,也必须要编码,否则第一步中url 带 的参数后台接收不到。编码的方式:在浏览器的开发者模式中的console中输入:encodeURIComponent('你的URL') 回车即可。

蓝色部分:如下参数说明

参数说明

参数 是否必须 说明
appid 公众号的唯一标识
redirect_uri 授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理
response_type 返回类型,请填写code
scope 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
#wechat_redirect 无论直接打开还是做页面302重定向时候,必须带此参数

第三步:将URL生成二维

可以百度“草料二维码”,可以利用网站生成二维码。

第四步:后台代码获取openid 。

思路:首先获取request中的code,然后通过code调用腾讯微信接口获取用户信息,其中就包括了openid。

由于小编使用的是struts2,所有构造了一个request对象。

public String queueInfo() throws Exception{
		HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);  
		String code = request.getParameter("code");
		//通过code获取openid;
		net.sf.json.JSONObject wxUser = CoreService.getOpenid(code);
		String openid = wxUser.getString("openid");
}
public class CoreService {
        public static String GETOPENID = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
        /*通过code获取用户openid*/
	public static JSONObject getOpenid(String code) throws IOException{
		JSONObject jsonObject = null;
		String path = GETOPENID.replace("APPID", APPID).replace("SECRET", APPSECRET).replace("CODE", code);  
		StringBuffer buffer = new StringBuffer();
                URL url = new URL(path);
                HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
                httpUrlConn.setRequestMethod("POST");
                httpUrlConn.setDoOutput(true);
                httpUrlConn.setDoInput(true);
                httpUrlConn.setUseCaches(false);
                    // 将返回的输入流转换成字符串
                InputStream inputStream = httpUrlConn.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String str = null;
                while ((str = bufferedReader.readLine()) != null) {
                    buffer.append(str);
                }
                bufferedReader.close();
                inputStreamReader.close();
                // 释放资源
                inputStream.close();
                inputStream = null;
                httpUrlConn.disconnect();
                jsonObject = JSONObject.fromObject(buffer.toString());
		return jsonObject;
	}
}




猜你喜欢

转载自blog.csdn.net/qq_33696345/article/details/80521077