หน้าเว็บ

วันอาทิตย์ที่ 14 กรกฎาคม พ.ศ. 2556

Google Authentication (Client Login) : java

GoogleAuthentication.java
package com.blogspot.na5cent.cloudprint.example;

import java.io.IOException;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author redcrow
 * @create 15/07/2013
 * @link http://na5cent.blogspot.com/2013/07/google-authentication-java.html
 */
public class GoogleAuthentication {

    private static final Logger LOG = LoggerFactory.getLogger(GoogleAuthentication.class);
    private static final String REQUEST_URL = "https://www.google.com/accounts/ClientLogin";
    private static final String ACCOUNT_TYPE = "HOSTED_OR_GOOGLE";
    //request by user
    private String serviceName;
    private String source;
    //response from google
    private String auth;
    private String sid;
    private String lsid;

    private GoogleAuthentication() {
    }

    public GoogleAuthentication(String serviceName) {
        this.serviceName = serviceName;
    }

    /**
     * For login Google Service<br/>
     * <a href='https://developers.google.com/accounts/docs/AuthForInstalledApps'>https://developers.google.com/accounts/docs/AuthForInstalledApps</a>
     *
     * @param email Google Account or Google Email
     * @param password Email Password
     * @param source Short string identifying your application, for logging
     * purposes. This string take from :
     * "companyName-applicationName-VersionID".
     * @throws IOException
     */
    public void login(String email, String password, String source) throws IOException {
        String request = REQUEST_URL
                + "?accountType=" + ACCOUNT_TYPE
                + "&Email=" + email
                + "&Passwd=" + password
                + "&service=" + serviceName
                + "&source=" + source;

        URL url = new URL(request);

        String response = ResponseUtils.streamtoString(url.openStream());
        LOG.debug("response => {}", response);

        String[] split = response.split("\n");
        for (String string : split) {
            String[] keyValueSplit = string.split("=");
            if (keyValueSplit.length == 2) {
                String key = keyValueSplit[0];
                String value = keyValueSplit[1];

                if (key.equals("Auth")) {
                    auth = value;
                } else if (key.equals("SID")) {
                    sid = value;
                } else if (key.equals("LSID")) {
                    lsid = value;
                }
            }
        }
    }

    public String getSource() {
        return source;
    }

    public String getServiceName() {
        return serviceName;
    }

    public String getAuth() {
        return auth;
    }

    public String getSid() {
        return sid;
    }

    public String getLsid() {
        return lsid;
    }
}
ResponseUtils.java
package com.blogspot.na5cent.cloudprint.example;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 *
 * @author redcrow
 * @create 15/07/2013
 * @link http://na5cent.blogspot.com/2013/07/google-authentication-java.html
 */
public class ResponseUtils {

    public static String streamtoString(InputStream inputStream) throws IOException {
        StringBuilder builder = new StringBuilder();
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            inputStreamReader = new InputStreamReader(inputStream);
            bufferedReader = new BufferedReader(inputStreamReader);

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                builder.append(line);
                builder.append("\r\n");
            }
        } catch (IOException ex) {
            throw ex;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    throw ex;
                }
            }

            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException ex) {
                    throw ex;
                }
            }

            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ex) {
                    throw ex;
                }
            }

            return builder.toString();
        }
    }
}

example to use
    GoogleAuthentication authen = new GoogleAuthentication('cloudprint');
    try{
        authen.login("example@gmail.com", "password1234", "na5cent-cloudprint-1.0");
    }catch(IOException ex){
        LOG.warn(null, ex);
    }

ไม่มีความคิดเห็น:

แสดงความคิดเห็น