Google Oauth Webflow Android
How can you authenticate using OAuth 2.0 with Google APIs? I have tried using the native dialog flow but that recently broken with the GoogleAuthUtil getToken() method returning d
Solution 1:
Make sure to register your application in the Google APIs Console.
Change the status to "On" for the APIs you wish to access.
google_oauth_webflow.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"
><WebViewandroid:id="@+id/webview"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1" /></LinearLayout>
arrays.xml
<?xml version="1.0" encoding="utf-8"?><resources><string-arrayname="scopes"><item>https://www.google.com/m8/feeds</item><item>https://www.googleapis.com/auth/userinfo.email</item><item>https://www.googleapis.com/auth/plus.login</item><item>https://www.googleapis.com/auth/userinfo.profile</item></string-array></resources>
GoogleOAuthWebFlowActivity.java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.biggu.shopsavvy.R;
import com.biggu.shopsavvy.utils.Toaster;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
publicclassGoogleOAuthWebFlowActivityextendsActivity {
private WebView webview;
privatestaticfinalStringTAG="Main";
private ProgressBar progressBar;
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE | Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.google_oauth_webflow);
this.webview = (WebView)findViewById(R.id.webview);
WebSettingssettings= webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.addJavascriptInterface(newCustomJavaScriptInterface(), "Android");
finalAlertDialogalertDialog=newAlertDialog.Builder(this).create();
setProgressBarIndeterminateVisibility(true);
webview.setWebViewClient(newWebViewClient() {
publicbooleanshouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
returntrue;
}
@OverridepublicvoidonPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
setProgressBarIndeterminateVisibility(false);
if(url.startsWith("https://accounts.google.com/o/oauth2/approval")){
Stringht="javascript:window.Android.retrieveExchangeCode(document.getElementsByTagName('title')[0].innerHTML);";
webview.loadUrl(ht);
}
}
publicvoidonReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toaster.makeToast("Oh no! " + description);
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton(getString(android.R.string.ok), newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
StringwebLoginClientId= getString(R.string.client_client_id);
Stringscope= TextUtils.join(" ", getResources().getStringArray(R.array.scopes));
Stringredirect_uri= getString(R.string.client_redirect_uri);
Stringurl="https://accounts.google.com/o/oauth2/auth?scope="+scope
+"&client_id="+webLoginClientId
+"&response_type=code&access_type=offline&approval_prompt=force&redirect_uri="+redirect_uri;
webview.loadUrl(url);
}
privateclassCustomJavaScriptInterface {
@JavascriptInterfacepublicvoidretrieveExchangeCode(String titleTag) {
StringcodePair= titleTag.substring(titleTag.indexOf("code="), titleTag.length());
StringcodeValue= codePair.substring(5, codePair.length());
IntentresultIntent=newIntent();
resultIntent.putExtra("exchangeCode", codeValue);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
}
}
Post a Comment for "Google Oauth Webflow Android"