I have a normal Java standalone app which is trying to communicate with
a GAE app. I make use of the HttpURLConnection class and all is jiffy.
But what I am having trouble doing is sending an http POST request
(with the URLConnection class) with an authentication token returned by
Google ClientLogin. I can't seem to be able to include the token in a
meaningful way. On the server-side, what I wish to do is identify the
user who is connecting from the stand-alone Java App.


For my Client-side code, there a couple things I have tried:

1) Sending the token directly in the Authentication header
2) Sending a cookie with the token as its value




p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco;
color: #4d9072} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px
Monaco; color: #3834ff} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font:
11.0px Monaco; color: #931a68} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px;
font: 11.0px Monaco} p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font:
11.0px Monaco; min-height: 15.0px} span.s1 {color: #000000} span.s2
{color: #931a68} span.s3 {color: #0027cc} span.s4 {color: #4d9072}
span.s5 {color: #3834ff} span.s6 {text-decoration: underline}
//AUTHENTICATE WITH GOOGLE

CalendarService client = new
CalendarService("https://www.google.com/accounts/ClientLogin";);

try {

client.setUserCredentials(userField.getText(), passField.getText());

} catch (AuthenticationException e1) {

e1.printStackTrace();

}







//STORE TOKEN LOCALLY

String token;

UserToken auth_token = (UserToken)
client.getAuthTokenFactory().getAuthToken();

token = auth_token.getValue();




[...]






p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco;
color: #4d9072} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px
Monaco; color: #3834ff} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font:
11.0px Monaco} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px
Monaco; min-height: 15.0px} p.p5 {margin: 0.0px 0.0px 0.0px 0.0px;
font: 11.0px Monaco; color: #931a68} span.s1 {color: #000000} span.s2
{color: #931a68} span.s3 {color: #3834ff} span.s4 {text-decoration:
underline} span.s5 {color: #0027cc}

//CREATE HTTP POST REQUEST

URL url = new URL("http://localhost:8888/playerConnect";);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setRequestMethod("POST");




//conn.setRequestProperty("Cookie", "auth=" + token);

conn.setRequestProperty("Cookie", cookie);



conn.setRequestProperty("Authorization", token);



//conn.setRequestProperty("Authorization", "Auth "+token);

conn.setRequestProperty("username", userField.getText());

conn.setDoOutput(true);

conn.connect();




//SEND DATA

ObjectOutputStream out = new ObjectOutputStream(new
BufferedOutputStream(conn.getOutputStream()));

out.writeObject(salvo);

out.flush();

out.close();




InputStreamReader in = new InputStreamReader(new
BufferedInputStream(conn.getInputStream()));


char[] string = new char[100];

in.read(string);




if(conn.getResponseCode() != 200) {

System.out.println("Succeeded");

}

else{

System.out.println("Response Code: " + conn.getResponseCode());

}




System.out.println(string);















On the server-side code, I try to get the user...




p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco} span.s1
{color: #931a68} span.s2 {text-decoration: underline}

public class PlayerServlet extends HttpServlet{

public void doPost(HttpServletRequest req, HttpServletResponse resp)

throws IOException{




p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco} p.p2
{margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height:
15.0px} span.s1 {text-decoration: underline} span.s2 {color: #3834ff}
span.s3 {color: #0027cc} span.s4 {color: #931a68}

Cookie[] cookies = req.getCookies();

String cookieVal = getCookieValue(cookies,"token","null");

System.out.println("CookieVal: " + cookieVal);



CalendarService client = new
CalendarService("https://www.google.com/accounts/ClientLogin";);

client.setUserToken(cookieVal);



if (req.getUserPrincipal() != null) {


System.out.println("username: " + req.getUserPrincipal().getName());

}

else{

System.out.println("Not signed in");


}




UserService userService = UserServiceFactory.getUserService();

User user = userService.getCurrentUser();

System.out.println("From user" + user);




[...]













And in web.xml, I have a security-constraint set up:




p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco;
color: #4d9192} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px
Monaco} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco;
min-height: 15.0px} span.s1 {color: #009193} span.s2 {color: #000000}
span.s3 {color: #4d9192}

<servlet>

<servlet-name>playerServlet</servlet-name>

<servlet-class>battleship.PlayerServlet</servlet-class>

</servlet>



<servlet-mapping>

<servlet-name>playerServlet</servlet-name>

<url-pattern>/playerConnect</url-pattern>

</servlet-mapping>






p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco;
color: #4d9192} span.s1 {color: #000000} span.s2 {color: #009193}
span.s3 {text-decoration: underline} span.Apple-tab-span
{white-space:pre}

<security-constraint>

<web-resource-collection>

<url-pattern>/playerConnect</url-pattern>

</web-resource-collection>

<auth-constraint>

<role-name>*</role-name>

</auth-constraint>

</security-constraint>









I get a couple of different behaviors, depending on my web.xml file. If
I don't specify any security-constraints in web.xml, the server outputs
that user is null.




When I do specify the security constraint in web.xml, the client gets a
HTTP 200 response code, and the last print statement (of string)
outputs some HTML text. The server prints no output; it's as if there
is no connection from the server's point of view.




I've tried all sorts of things with my HttpURLConnection stuff
client-side, and nothing seems to work. Again, all I want to do is have
a user authenticate with google, and connect to my GAE app with their
login information. Thanks in advance for any helps.

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to