Created
December 30, 2010 11:18
-
-
Save shin1ogawa/759684 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
import java.util.*; | |
import java.util.logging.*; | |
public class ClientLoginUtil { | |
static final Logger logger = Logger.getLogger(ClientLoginUtil.class.getName()); | |
static final String CLIENTLOGIN_URL = "https://www.google.com/accounts/ClientLogin"; | |
/** | |
* @param email | |
* @param password | |
* @param applicationName | |
* {yourname}-{applicationName}-{versionNumber} | |
* @param applicationUrl url of your application | |
* e.g. {@literal http://shin1ogawa.appspot.com/} | |
* @param continueUrl url of needs authenticate | |
* e.g. {@literal http://shin1ogawa.appspot.com/pathToNeedsAuthenticate} | |
* @return cookie value {@literal ACSID=....} for http or {@literal SACSID=....} for https | |
* @throws IOException | |
*/ | |
public static String getCookie(String email, String password, | |
String applicationName, String applicationUrl, String continueUrl) throws IOException { | |
String authToken = getAuthTokenUsingGoogleClientLogin(email, password, applicationName); | |
return getCookieUsingAppengineLogin(applicationUrl, continueUrl, authToken); | |
} | |
static String getAuthTokenUsingGoogleClientLogin(String email, | |
String password, String applicationName) throws IOException { | |
URL url = new URL(CLIENTLOGIN_URL); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("POST"); | |
connection.setDoOutput(true); | |
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); | |
PrintWriter w = new PrintWriter(new OutputStreamWriter(connection.getOutputStream())); | |
w.print("Email=" + URLEncoder.encode(email, "utf-8")); | |
w.print("&Passwd=" + URLEncoder.encode(password, "utf-8")); | |
w.print("&service=" + URLEncoder.encode("ah", "utf-8")); | |
w.print("&source=" + URLEncoder.encode(applicationName, "utf-8")); | |
w.print("&accountType=" + URLEncoder.encode("HOSTED_OR_GOOGLE", "utf-8")); | |
w.flush(); | |
w.close(); | |
connection.connect(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader( | |
connection.getInputStream())); | |
String line = null; | |
while ((line = reader.readLine()) != null) { | |
if (line == null || line.startsWith("Auth") == false) { | |
continue; | |
} | |
return line.substring(line.indexOf('=') + 1); | |
} | |
throw new AssertionError(); | |
} | |
static String getCookieUsingAppengineLogin(String applicationUrl, | |
String continueUrl, String authToken) throws IOException { | |
StringBuilder _url = new StringBuilder(applicationUrl) | |
.append(applicationUrl.endsWith("/") ? "" : "/") | |
.append("_ah/login?").append("continue=") | |
.append(URLEncoder.encode(continueUrl, "utf-8")).append("&") | |
.append("auth=").append(URLEncoder.encode(authToken, "utf-8")); | |
if (logger.isLoggable(Level.FINE)) { | |
logger.log(Level.FINE, "GET " + _url); | |
} | |
URL url = new URL(_url.toString()); | |
boolean followRedirects = HttpURLConnection.getFollowRedirects(); | |
HttpURLConnection.setFollowRedirects(false); | |
try { | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestProperty("X-appcfg-api-version", "dummy"); | |
connection.connect(); | |
if (logger.isLoggable(Level.FINE)) { | |
logger.log(Level.FINE, "responseCode=" + connection.getResponseCode()); | |
} | |
Map<String, List<String>> headers = connection.getHeaderFields(); | |
Iterator<Map.Entry<String, List<String>>> i = headers.entrySet().iterator(); | |
while (i.hasNext()) { | |
Map.Entry<String, List<String>> next = i.next(); | |
String key = next.getKey(); | |
if (key == null || key.equals("Set-Cookie") == false) { | |
continue; | |
} | |
List<String> values = next.getValue(); | |
if (values.isEmpty() == false) { | |
return values.get(0); | |
} else { | |
return null; | |
} | |
} | |
return null; | |
} finally { | |
HttpURLConnection.setFollowRedirects(followRedirects); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment