package api.http.google;
import api.KeyValuePair;
import api.http.HttpQuery;
import api.http.HttpRequest;
import java.io.IOException;
import java.net.URL;
public class GoogleAPI{
/**
* a Picasa service.
*/
public static final String Picasa = "lh2";
private String user;
private String password;
private String service;
private String source;
private String authToken;
/**
* constructs this object with an exist auth token.
*
* @param authToken an exist auth token.
*/
public GoogleAPI(String authToken){
this.authToken = authToken;
}
/**
* constructs this object with user, password, service and source.
*
* @param user a google account.
* @param password the password login for.
* @param service the service to use.
* @param source the source of these actions.
* @throws IOException the url is not exist or something wrong of the server
* of target url.
*/
public GoogleAPI(String user, String password, String service, String source) throws IOException{
this.user = user;
this.password = password;
this.service = service;
this.source = source;
this.login();
}
/**
* returns the user.
*
* @return the user.
*/
public String getUser(){
return user;
}
/**
* returns the service.
*
* @return the service.
*/
public String getService(){
return service;
}
/**
* returns the source.
*
* @return the source.
*/
public String getSource(){
return source;
}
/**
* returns the auth token if passing and exist auth token of login
* successful.
*
* @return the auth token if passing and exist auth token of login
* successful.
*/
public String getAuthToken(){
return authToken;
}
/**
* returns the auth token convert into KeyValuePair object for GoogleLogin
* header.
*
* @return the auth token convert into KeyValuePair object for GoogleLogin
* header.
*/
public KeyValuePair toAuthHeader(){
return new KeyValuePair("Authorization", String.format("GoogleLogin auth=\"%s\"", this.authToken));
}
private void login() throws IOException{
HttpRequest httpRequest = new HttpRequest(new URL("https://www.google.com/accounts/ClientLogin"));
HttpQuery httpQuery = new HttpQuery();
httpQuery.add("Email", this.user);
httpQuery.add("Passwd", this.password);
httpQuery.add("service", this.service);
httpQuery.add("source", this.source);
httpRequest.setData(httpQuery.toString());
String response = httpRequest.send();
String responses[] = response.split("\n");
String authHeader = "Auth=";
for (String r : responses){
if (r.indexOf(authHeader) == 0){
this.authToken = r.substring(authHeader.length());
}
}
}
}