1. 程式人生 > >微信第三方平臺開發經驗總結(五):接收授權方授權請求並儲存授權方資訊

微信第三方平臺開發經驗總結(五):接收授權方授權請求並儲存授權方資訊

public String queryAuth(String authCode,String expiresIn) {     String componentAccessToken = getComponentAccessToken();//把之前儲存的component_access_token取出來     logger.info("ThirdPartyServiceImpl:queryAuth:componentAccessToken={}",componentAccessToken);     /**替換Url中的{component_access_token}*/     String url = ThirdPartyConfig.QUERY_AUTH_URL.replace("{component_access_token}",componentAccessToken);     logger.info("ThirdPartyServiceImpl:getPreAuthCode:url={}",url);      /**拼json*/     JSONObject json = new JSONObject();     json.accumulate("component_appid",ThirdPartyConfig.APP_ID);     json.accumulate("authorization_code",authCode);     logger.info("ThirdPartyServiceImpl:getPreAuthCode:json={}",json);     /**傳送Https請求到微信*/     String retStr = HttpsUtil.postHtpps(url,json.toString());     logger.info("ThirdPartyServiceImpl:getPreAuthCode:retStr={}",retStr);     JSONObject resultJson = JSONObject.fromObject(retStr);     JSONObject infoJson = resultJson.getJSONObject("authorization_info");     /**在返回結果中獲取資訊*/     String authorizerAccessToken = infoJson.getString("authorizer_access_token");     String authorizerRefreshToken = infoJson.getString("authorizer_refresh_token");     String authorizerAppid = infoJson.getString("authorizer_appid");     logger.info("queryAuth:authorizer_appid={}",authorizerAppid);     logger.info("queryAuth:authorizer_access_token={}",authorizerAccessToken);     logger.info("queryAuth:authorizer_refresh_token={}",authorizerRefreshToken);      /**把authorizer_access_token和authorizer_refresh_token儲存到資料庫中*/     authorizerDBService.setexAccessToken(authorizerAppid, authorizerAccessToken,7200);     authorizerDBService.setRefreshToken(authorizerAppid, authorizerRefreshToken);      /**通過authorizerAppid呼叫Api獲取更詳細的資訊*/     setAuthorizerInfo(authorizerAppid);     return authorizerAppid; }