@Test public void testGetAccessToken() throws URISyntaxException, IOException { AuthManager manager = new WeiboAuthManager(new AuthInfo("", "", "http: URI uri = new URI(manager.getAuthUrl()); Desktop.getDesktop().browse(uri); String accessToken = manager.getAccessToken(new Scanner(System.in).nextLine()).getKey(); UserInfo info = manager.getUserInfo(accessToken); System.out.println(info.getId()); System.out.println(info.getUsername()); }
@Override public Pair<String, Long> getAccessToken(String code) { tokenUrl = tokenUrl + "&code=" + code; String result = ""; String accessToken = ""; long timeout = 0l; try { result = this.post(tokenUrl, new ArrayList<>()); if (StringUtils.isNotBlank(result)) { JSONObject obj = new JSONObject(result); try { String error = obj.getString("error"); logger.info("Result is not valid: result=[" + result + "], error is" + error); throw new NullPointerException("Error occurred: " + error); } catch (JSONException e) { accessToken = obj.getString("access_token"); timeout = obj.getLong("expires_in"); } } else { throw new NullPointerException("Result is null while getting token"); } } catch (IOException e) { logger.warning("Failed to get accesstoken, url=[" + tokenUrl + "], code=[" + code + "], error is: \n" + e); } return new Pair<>(accessToken, timeout); }
WeiboAuthManager extends AuthManager { @Override public Pair<String, Long> getAccessToken(String code) { tokenUrl = tokenUrl + "&code=" + code; String result = ""; String accessToken = ""; long timeout = 0l; try { result = this.post(tokenUrl, new ArrayList<>()); if (StringUtils.isNotBlank(result)) { JSONObject obj = new JSONObject(result); try { String error = obj.getString("error"); logger.info("Result is not valid: result=[" + result + "], error is" + error); throw new NullPointerException("Error occurred: " + error); } catch (JSONException e) { accessToken = obj.getString("access_token"); timeout = obj.getLong("expires_in"); } } else { throw new NullPointerException("Result is null while getting token"); } } catch (IOException e) { logger.warning("Failed to get accesstoken, url=[" + tokenUrl + "], code=[" + code + "], error is: \n" + e); } return new Pair<>(accessToken, timeout); } }
WeiboAuthManager extends AuthManager { @Override public Pair<String, Long> getAccessToken(String code) { tokenUrl = tokenUrl + "&code=" + code; String result = ""; String accessToken = ""; long timeout = 0l; try { result = this.post(tokenUrl, new ArrayList<>()); if (StringUtils.isNotBlank(result)) { JSONObject obj = new JSONObject(result); try { String error = obj.getString("error"); logger.info("Result is not valid: result=[" + result + "], error is" + error); throw new NullPointerException("Error occurred: " + error); } catch (JSONException e) { accessToken = obj.getString("access_token"); timeout = obj.getLong("expires_in"); } } else { throw new NullPointerException("Result is null while getting token"); } } catch (IOException e) { logger.warning("Failed to get accesstoken, url=[" + tokenUrl + "], code=[" + code + "], error is: \n" + e); } return new Pair<>(accessToken, timeout); } WeiboAuthManager(AuthInfo info); }
WeiboAuthManager extends AuthManager { @Override public Pair<String, Long> getAccessToken(String code) { tokenUrl = tokenUrl + "&code=" + code; String result = ""; String accessToken = ""; long timeout = 0l; try { result = this.post(tokenUrl, new ArrayList<>()); if (StringUtils.isNotBlank(result)) { JSONObject obj = new JSONObject(result); try { String error = obj.getString("error"); logger.info("Result is not valid: result=[" + result + "], error is" + error); throw new NullPointerException("Error occurred: " + error); } catch (JSONException e) { accessToken = obj.getString("access_token"); timeout = obj.getLong("expires_in"); } } else { throw new NullPointerException("Result is null while getting token"); } } catch (IOException e) { logger.warning("Failed to get accesstoken, url=[" + tokenUrl + "], code=[" + code + "], error is: \n" + e); } return new Pair<>(accessToken, timeout); } WeiboAuthManager(AuthInfo info); @Override String getAuthUrl(); @Override Pair<String, Long> getAccessToken(String code); @Override String refresh(String token); @Override UserInfo getUserInfo(String accessToken); @Override boolean cancel(String accessToken); }
WeiboAuthManager extends AuthManager { @Override public Pair<String, Long> getAccessToken(String code) { tokenUrl = tokenUrl + "&code=" + code; String result = ""; String accessToken = ""; long timeout = 0l; try { result = this.post(tokenUrl, new ArrayList<>()); if (StringUtils.isNotBlank(result)) { JSONObject obj = new JSONObject(result); try { String error = obj.getString("error"); logger.info("Result is not valid: result=[" + result + "], error is" + error); throw new NullPointerException("Error occurred: " + error); } catch (JSONException e) { accessToken = obj.getString("access_token"); timeout = obj.getLong("expires_in"); } } else { throw new NullPointerException("Result is null while getting token"); } } catch (IOException e) { logger.warning("Failed to get accesstoken, url=[" + tokenUrl + "], code=[" + code + "], error is: \n" + e); } return new Pair<>(accessToken, timeout); } WeiboAuthManager(AuthInfo info); @Override String getAuthUrl(); @Override Pair<String, Long> getAccessToken(String code); @Override String refresh(String token); @Override UserInfo getUserInfo(String accessToken); @Override boolean cancel(String accessToken);  }