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:
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?
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With