java applet to log micro letter

 Log in micro letter

 

Development preparation before (must)

  Applet identification (appid): wx4d4838ebec29b8 **

  Applet secret key (secret): 4fd6ca38a1261f96cbc0314c5675b9 **

 

Log in micro-channel official website:  https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html 

According to official documents

 

 

 

 

 Interface login java

// pass the front end of your code  
public void wxlogin (String code) {
     // fill in your appid small and secret procedures, as well as the front passed to your code, the last parameter is fixed String TOKEN_URL
= "HTTPS: // api.weixin.qq.com/sns/jscode2session?appid= "+ WxConfig.APP_ID +" Secret = & "+ WxConfig.SECRET +" & js_code = "+ code +" = authorization_code & grant_type " ; the JSONObject the access_token = httpsRequestToJsonObject (TOKEN_URL," GET ", null );
     
// openid String that uniquely identifies the user this is your micro-channel user ID String openid = access_token.getString (" openid " ); // session_key String session key String session_key = access_token.getString("session_key"); // errcode number 错误码 String errcode = access_token.getString("errcode"); // errmsg string 错误信息 String errmsg = access_token.getString("errmsg"); if ("-1".equals(errcode)) { System.err.println(errmsg); return; } if ("4029".equals(errcode)) { System.err.println(errmsg); return; } if ("45011".equals(errcode)) { System.err.println(errmsg); return ; } the else { // prove no problem // save openid to your database, call your service business } } public static JSONObject httpsRequestToJsonObject (String requestUrl, requestMethod String, String outputStr) { JSONObject jsonObject = null ; the try { StringBuffer Buffer = httpsRequest (requestUrl, requestMethod, outputStr); jsonObject = JSONObject.fromObject (buffer.toString ()); } the catch (a ConnectException CE) { System.err.println ("请求连接超时"+ce); } catch (Exception e) { System.err.println("https请求异常:" + e.getMessage()); } return jsonObject; } private static StringBuffer httpsRequest(String requestUrl, String requestMethod, String output) throws NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException, MalformedURLException, IOException, ProtocolException, UnsupportedEncodingException { URL url = new URL(null, requestUrl, new Handler()); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestMethod(requestMethod); if (null != output) { OutputStream outputStream = connection.getOutputStream(); outputStream.write(output.getBytes("UTF-8")); outputStream.close(); } // 从输入流读取返回内容 InputStream inputStream = connection.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; connection.disconnect(); return buffer; }

 

 If appid and secret hundred percent right, then this would be no problem!

Guess you like

Origin www.cnblogs.com/lccsdncnblogs/p/11443373.html