0

我正在尝试OAuth通过 android 应用程序实现实时连接的流程。对于身份验证和同意部分,我使用WebView将用户重定向到相应的页面。我试图实现的流程是 -

  1. 启动MyActivity
  2. onCreate()中,启动auth url并等待。
  3. 用户使用帐户登录并被重定向到同意页面。
  4. 用户同意访问权限。
  5. 解析授权码。
  6. 返回并使用代码MyActivity执行操作。auth

下面是代码片段:

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;
            } 

我坚持要MyActivity等到步骤 3-5 完成。请提出替代方案来实施这种流程。

4

1 回答 1

2

如下所示更新了我的 WebViewClient 实现,解决了问题。希望有人觉得这很有用。

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();
            }
        }
    });
于 2013-03-04T16:44:01.857 回答