Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Access token server side validation for android app

I'm developing android application, that is based on communication with server, and I want to use Google(g+) authentication mechanisms.

Basically, I think it should work like this:

  1. In my android app, user logs in to Google, using his email and password.
  2. User allows access to his data for related Google application.
  3. My android app receives access token, after successful log in.
  4. In further communication with my server, my android application should using received Google access token (for example: in queries).
  5. When my server receives some query from android app, with access token, it should ask Google that this token is valid (and for who), and if yes, server should assume that user is authenticated with Google.

My question is: how the server should ask Google if given access token is valid? I think I should somehow check if the token is valid for my android app.

I've tried many Google queries to Google API, that I've found, but nothing worked as I expected. Can you provide me some example?

like image 580
Bhargav Methuku Avatar asked Oct 20 '13 13:10

Bhargav Methuku


1 Answers

You can validate if the access_token is valid or not.

You need to send GET request to the api end point : https://www.googleapis.com/oauth2/v1/tokeninfo with your access_token in request.

You can try it like this:

 String connection = new ConnectionService().connectionGoogle("https://www.googleapis.com/oauth2/v1/tokeninfo", "access_token=" + "YOUR_EXISTING_ACCESS_TOKEN");
        System.out.println(connection);

The methods used in above code are:

public static String connectionGoogle(String url, String parameter) throws MalformedURLException, ProtocolException, IOException {

        URL url1 = new URL(url);
        HttpURLConnection request1 = (HttpURLConnection) url1.openConnection();
        request1.setRequestMethod("GET");
        request1.setDoOutput(true);
        request1.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        OutputStreamWriter wr = new OutputStreamWriter(request1.getOutputStream());
        wr.write(parameter);
        wr.flush();
        request1.connect();
        String responseBody = convertStreamToString(request1.getInputStream());
        wr.close();
        return responseBody;
    }

    private static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } catch (IOException e) {
        } finally {
            try {
                is.close();
            } catch (IOException e) {
            }
        }

        return sb.toString();
    }
like image 79
Jhanvi Avatar answered Oct 19 '22 22:10

Jhanvi