Skip to content

Instantly share code, notes, and snippets.

@omerucel
Created May 23, 2016 06:03
Show Gist options
  • Save omerucel/da93e3632f367408091e0ba32cff7711 to your computer and use it in GitHub Desktop.
Save omerucel/da93e3632f367408091e0ba32cff7711 to your computer and use it in GitHub Desktop.
Google Sign in sample with webview
package com.omerucel.sample;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import com.omerucel.sample.CustomApplication;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class GoogleSignInActivity extends AppCompatActivity {
final public static String CLIENT_ID = "";
protected CustomApplication customApplication;
protected OkHttpClient okHttpClient;
protected Boolean firstPageInitialize = false;
protected WebView webView;
protected TextView loadingMessage;
protected TextView successMessage;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_signin);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
customApplication = (CustomApplication) getApplication();
webView = (WebView) findViewById(R.id.webview);
loadingMessage = (TextView) findViewById(R.id.loading_message);
successMessage = (TextView) findViewById(R.id.success_message);
okHttpClient = new OkHttpClient();
String url = "https://accounts.google.com/o/oauth2/v2/auth?"
+ "scope=https://www.googleapis.com/auth/drive.appdata"
+ "&redirect_uri=http://localhost"
+ "&response_type=code"
+ "&client_id=" + CLIENT_ID;
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d("test", "onPageStarted:" + url);
Log.d("test", "onPageStarted:" + view.getTitle());
Uri uri = Uri.parse(url);
if (uri.getHost().equals("localhost")) {
webView.setVisibility(View.INVISIBLE);
loadingMessage.setVisibility(View.INVISIBLE);
successMessage.setVisibility(View.VISIBLE);
}
if (uri.getQueryParameter("code") != null && uri.getQueryParameter("code").length() > 0) {
handleAccessCode(uri.getQueryParameter("code"));
}
if (uri.getQueryParameter("error") != null) {
Log.d("test", "onPageStarted:" + uri.getQueryParameter("error"));
loadingMessage.setText(getString(R.string.an_error_occurred));
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("test", "onPageFinished:" + url);
Log.d("test", "onPageFinished:" + view.getTitle());
if (!firstPageInitialize) {
firstPageInitialize = true;
loadingMessage.setVisibility(View.INVISIBLE);
webView.setVisibility(View.VISIBLE);
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Log.d("test", "onReceiveError");
loadingMessage.setText(getString(R.string.an_error_occurred));
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
public void handleAccessCode(String accessCode) {
String content = "code=" + accessCode
+ "&client_id=" + CLIENT_ID
+ "&redirect_uri=http://localhost"
+ "&grant_type=authorization_code";
RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"), content);
Request request = new Request.Builder()
.url("https://www.googleapis.com/oauth2/v4/token")
.post(requestBody)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("test", "onFailure:" + e.getMessage(), e);
loadingMessage.setText(getString(R.string.an_error_occurred));
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
Log.d("test", "Response: " + response.body().string());
customApplication.preferences.saveAccessToken(jsonObject.getString("access_token"));
finish();
} catch (JSONException e) {
Log.d("test", "onResponse:" + e.getMessage(), e);
loadingMessage.setText(getString(R.string.an_error_occurred));
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment