0

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 -

  1. Launch MyActivity.
  2. In onCreate(), launch auth url and wait.
  3. User logs in using an account and is redirected to consent page.
  4. User agrees to access permissions.
  5. Parse auth code.
  6. Return to 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.

1 Answer 1

2

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();
            }
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.