I am trying to import gmail contacts into my java web application. I followed the document https://developers.google.com/identity/protocols/OAuth2WebServer?csw=1#callinganapi
I am able to authenticate gmail user but when i click accept, when asked for manage your contacts i get error:
java.net.ConnectException: Connection timed out
i am calling this URL:
and when i land on call.jsp of my web application it shows error on line:
client.executeMethod(post);
Here is my code on call.jsp:
<%@page import="org.jsoup.select.Elements"%>
<%@page import="org.jsoup.Jsoup"%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.jsoup.nodes.Document"%>
<%@page import="org.apache.commons.httpclient.methods.GetMethod"%>
<%@page import="org.json.JSONObject"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="org.apache.commons.httpclient.NameValuePair"%>
<%@page import="org.apache.commons.httpclient.methods.PostMethod"%>
<%@page import="org.apache.commons.httpclient.HttpClient"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <%
         String code = request.getParameter("code");
      System.out.println(code);
      HttpClient client = new HttpClient();
      PostMethod post = new PostMethod("https://accounts.google.com/o/oauth2/token");
      post.addRequestHeader("Host", "accounts.google.com");
      post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      NameValuePair[] data = {
                new NameValuePair("code", code),
                new NameValuePair("client_id", "XXXXX"),
                new NameValuePair("client_secret", "XXXXX"),
                new NameValuePair("redirect_uri", "http://XXX/call.jsp"),
                new NameValuePair("grant_type", "authorization_code")
      };
      post.setRequestBody(data);
            client.executeMethod(post);
            BufferedReader b = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
             StringBuilder sb = new StringBuilder();
            String str = null;
            while ((str = b.readLine()) != null) {
                sb.append(str);
            }
            JSONObject access_token = new JSONObject(sb.toString());
            GetMethod get = new GetMethod("https://www.google.com/m8/feeds/contacts/default/full?max-results=1000&access_token=" + access_token.getString("access_token"));
                client.executeMethod(get);
                b = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
                sb = new StringBuilder();
                str = null;
                while ((str = b.readLine()) != null) {
                    sb.append(str);
                }
                Document doc = null;
             //Here you will get contacts in xml format.  
             out.print(sb.toString());
             // Using Jsoup you can read data from xml
    %>
</body>
</html>
Please suggest, what i am doing wrong?
 
     
     
     
    