天天看點

微信二維碼掃碼擷取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;
	}
}
           

繼續閱讀