CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost tokenPost = new HttpPost(TOKEN_URL);
tokenPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
tokenPost.setEntity(new StringEntity(entity, "UTF-8"));
CloseableHttpResponse resp1 = httpclient.execute(tokenPost);
ObjectReader jreader = Utils.getJsonReader(Map.class);
if (resp1 != null && resp1.getEntity() != null) {
Map<String, Object> token = jreader.readValue(resp1.getEntity().getContent());
if (token != null && token.containsKey("access_token")) {
// got valid token
HttpGet profileGet = new HttpGet(PROFILE_URL);
profileGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token.get("access_token"));
CloseableHttpResponse resp2 = httpclient.execute(profileGet);
HttpEntity respEntity = resp2.getEntity();
String ctype = resp2.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
if (respEntity != null && Utils.isJsonType(ctype)) {
Map<String, Object> profile = jreader.readValue(resp2.getEntity().getContent());
if (profile != null && profile.containsKey("sub")) {
String googleSubId = (String) profile.get("sub");
String pic = (String) profile.get("picture");
String email = (String) profile.get("email");