/** * 获取未授权的Request Token * * @param oauth * @return * @throws Exception */ public OAuth requestToken(OAuth oauth) throws Exception { String url = "https://open.t.qq.com/cgi-bin/request_token"; QHttpClient http = new QHttpClient(); String queryString = getOauthParams(url, "GET", oauth.getOauth_consumer_secret(), "", oauth.getParams()); log.info("requestToken queryString = " + queryString); System.out.println("queryString:" + queryString); String responseData = http.httpGet(url, queryString); log.info("requestToken responseData = " + responseData); System.out.println("responseData:" + responseData); if (!parseToken(responseData, oauth)) { // Request Token 授权不通过 oauth.setStatus(1); log.info("requestToken past !"); } return oauth; }
/** * 使用授权后的Request Token换取Access Token * * @param oauth * @return * @throws Exception */ public OAuth accessToken(OAuth oauth) throws Exception { log.info( "accessToken oauth.getOauth_token() = " + oauth.getOauth_token() + "oauth.getOauth_verifier() = " + oauth.getOauth_verifier()); String url = "https://open.t.qq.com/cgi-bin/access_token"; QHttpClient http = new QHttpClient(); String queryString = getOauthParams( url, "GET", oauth.getOauth_consumer_secret(), oauth.getOauth_token_secret(), oauth.getAccessParams()); log.info("accessToken queryString = " + queryString); log.info("accessToken url = " + url); String responseData = http.httpGet(url, queryString); log.info("accessToken responseData = " + responseData); if (!parseToken(responseData, oauth)) { // Access Token 授权不通过 oauth.setStatus(2); } return oauth; }
/** * 验证Token返回结果 * * @param response * @param oauth * @return * @throws Exception */ public boolean parseToken(String response, OAuth oauth) throws Exception { if (response == null || response.equals("")) { return false; } oauth.setMsg(response); String[] tokenArray = response.split("&"); if (tokenArray.length < 2) { return false; } String strTokenKey = tokenArray[0]; String strTokenSecrect = tokenArray[1]; String[] token = strTokenKey.split("="); if (token.length < 2) { return false; } oauth.setOauth_token(token[1]); String[] tokenSecrect = strTokenSecrect.split("="); if (tokenSecrect.length < 2) { return false; } oauth.setOauth_token_secret(tokenSecrect[1]); log.info("parseToken response=>> tokenArray.length = " + tokenArray.length); if (tokenArray.length == 3) { String[] params = tokenArray[2].split("="); if ("name".equals(params[0]) && params.length == 2) { // oauth.setAccount(this.getAccount(oauth));// 获取当前登录用户的账户信息 } } return true; }