Reputation: 244
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?
Upvotes: 0
Views: 623
Reputation: 3532
If I correctly understood, you are able to get the authorization code
, but no the token.
Your post
object is using the URL value https://accounts.google.com/o/oauth2/token
, but according the documentation you've linked, to fetch the token
, you must use https://googleapis.com/oauth2/v3/token
Instead of try to re-invent the wheel, why not use libraries like gdata-java-client or CloudSponge?
They encapsulate all details for you, and have several samples.
Upvotes: 1
Reputation: 955
First of all, it's not a good idea to inline Java code in the JSP page. See the answer to this question to understand why.
As far as I can tell, you are using the same JSP to make the OAuth request and handle the callback as a redirect URI. I am guessing that you were trying to follow this tutorial. As you can see, that tutorial uses the Struts framework to separate the controller logic (making the OAuth request with a Struts Action) from view logic (displaying the contacts list).
Try to follow the tutorial in its entirety, and then make customizations and adjustments according to your needs.
Upvotes: 0