如何利用wx.login方法獲取openid和sessionKey


1,

wx.login(Object object)

調用接口獲取登錄憑證(code)。通過憑證進而換取用戶登錄態信息,包括用戶的唯一標識(openid)及本次登錄的會話密鑰(session_key)等。用戶數據的加解密通訊需要依賴會話密鑰完成。

2,

  1. 調用 wx.login() 獲取 臨時登錄憑證code (五分鍾有效期),並回傳到開發者服務器。
  2. 調用 auth.code2Session 接口,換取 用戶唯一標識 OpenID會話密鑰 session_key

3,登錄憑證校驗。通過 wx.login 接口獲得臨時登錄憑證 code 后傳到開發者服務器調用此接口完成登錄流程。

請求地址

GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_cod

4,后台獲取openid和sessionkey代碼
@RequestMapping(value="/getAppid",method = RequestMethod.GET)
    @ResponseBody
    public  String do_get(String JSCODE) throws ClientProtocolException, IOException {
        String body = "{}";
        String APPID = "wxbea50118b27deed2";
        String SECRET = "916e340c613a39a413076a03d5379692";
//        String JSCODE1 = "021Ktl4S1ognG510zT2S1f7w4S1Ktl4Q";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            HttpGet httpget = new HttpGet("https://api.weixin.qq.com/sns/jscode2session?appid="+APPID+"&secret="+SECRET+"&js_code="+JSCODE+"&grant_type=authorization_code");
//            String a = "https://api.weixin.qq.com/sns/jscode2session?appid="+APPID+"&secret="+SECRET+"&js_code="+JSCODE+"&grant_type=authorization_code";
//            System.out.println(a);
//解決ssl證書問題
SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); body = EntityUtils.toString(entity); System.out.println(body); } finally { httpclient.getConnectionManager().shutdown(); } return body; }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM