Reputation: 519
I am trying to implment the OAuth
flow for live connect through an android app. For the authentication and consent part, I am using WebView
to redirect the user to the corresponding pages. The flow that I am trying to implement is -
MyActivity
.onCreate()
, launch auth url
and wait.MyActivity
and perform actions using auth
code.Below is the code snippet :
public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
WebView myWebView = (WebView) findViewById(R.id.webview);
AuthFlowWebView authView = new AuthFlowWebView(); //AuthFlowWebView extends WebViewClient
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(authView);
myWebView.loadUrl("https://login.live.com/oauth20_authorize.srf?client_id=<CLIENT_ID>&scope=wl.signin%20wl.offline_access&response_type=code&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf&display=touch");
Log.i("", "Here already before the auth process is complete");
}
}
//Here is the Overriden onPageFinished method used to parse the auth code in AuthFlowWebView class:
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Thread.dumpStack();
if (url.contains("oauth20_desktop.srf?code=")) {
authSuccess = true;
Uri uri = Uri.parse(url);
authCode = uri.getQueryParameter("code");
Log.i("", "CODE : " + authCode);
authProcessComplete = true;
}
I am stuck with making MyActivity
wait until steps 3-5 are complete. Please suggest alternatives to implement such a flow.
Upvotes: 0
Views: 5050
Reputation: 519
Updated my WebViewClient implementation as given below, that fixed the problem. Hope someone finds this useful.
myWebView.setWebViewClient(new WebViewClient() {
boolean authComplete = false;
Intent resultIntent = new Intent();
@Override public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
pDialog = ProgressDialog.show(view.getContext(), "",
"Connecting to " + provider + " server", false);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pDialog.dismiss();
if (url.contains("?code=") && authComplete != true) {
Uri uri = Uri.parse(url);
authCode = uri.getQueryParameter("code");
Log.i("", "CODE : " + authCode);
authComplete = true;
resultIntent.putExtra("code", authCode);
WebActivity.this
.setResult(Activity.RESULT_OK, resultIntent);
resultIntent.putExtra("status", WebActivity.Status.SUCCESS.toString());
setResult(Activity.RESULT_CANCELED, resultIntent);
finish();
}else if(url.contains("error=access_denied")){
Log.i("", "ACCESS_DENIED_HERE");
resultIntent.putExtra("code", authCode);
resultIntent.putExtra("status", WebActivity.Status.ACCESS_DENIED.toString());
authComplete = true;
setResult(Activity.RESULT_CANCELED, resultIntent);
finish();
}
}
});
Upvotes: 2