Tuesday, April 16, 2013

google Authentication for Android Applications

public class GoogleAuthenticationWithLoginDetails {
    private final String googleLoginUrl = "https://www.google.com/accounts/ClientLogin";
    private final String service = "ah";
    private Cookie authCookie = null;

    //Constructor creates the cookie
    public GoogleAuthenticationWithLoginDetails(){
       
    }


    public boolean authenticate(String username,String password){
        boolean returnValue = false;
        String authToken = null;
        if (authCookie == null)
        {

            Log.d("authenticate", "NULL");

            try {
                authToken = GetToken(username,password);

                //authCookie = getAuthCookie(authToken);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.d("authenticate Exception", e.getMessage());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.d("authenticate Exception", e.getMessage());
            }


        }
       
        if(authToken.trim().length()>0){
            Log.d("authToken!=null","NOT NULL");
            Log.d("TOKEN=", authToken);
            returnValue =  true;
        }else{
            Log.d("authToken==null","NULL");
            returnValue =  false;
        }
        return returnValue;
    }


    private String GetToken(String googleAccount, String googlePassword) throws MalformedURLException, ProtocolException, UnsupportedEncodingException, IOException
    {
        Log.d("GetToken","Reached");
        String token = null;
        HttpURLConnection h = GetConnection(googleAccount,googlePassword);
        token= extractAuthTokenFromResponse(h);
        Log.d("GetToken",token);
        return token;
    }



    private HttpURLConnection GetConnection(String username, String password)
            throws MalformedURLException, IOException, ProtocolException,
            UnsupportedEncodingException {
        HttpURLConnection urlConnection;
        URL url = new URL(googleLoginUrl);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setUseCaches(false);
        urlConnection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        StringBuilder content = new StringBuilder();
        content.append("Email=").append(username);
        content.append("&Passwd=").append(password);
        content.append("&service=").append(service);

        OutputStream outputStream = urlConnection.getOutputStream();
        outputStream.write(content.toString().getBytes("UTF-8"));
        outputStream.close();
        return urlConnection;
    }
    private String extractAuthTokenFromResponse(HttpURLConnection urlConnection)
            throws IOException {
        int responseCode = urlConnection.getResponseCode();
        System.out.println(responseCode);
        StringBuffer resp = new StringBuffer();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = urlConnection.getInputStream();

            BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
            String line;


            while ((line = rd.readLine()) != null) {

                if(line.startsWith("Auth="))
                {
                    resp.append(line.substring(5));

                }

            }

            rd.close();


        }
        return resp.toString();
    }
}

No comments:

Post a Comment