android - 使用 WebView 进行 OAuth2 授权

标签 android webview

我正在使用 webview 进行 OAuth 授权以进行登录。 我知道这个问题被问了很多次,但没有得到任何适当的例子。 所以我的问题是我无法获得 authCode。 查看我的代码:

最终对话框 webDialog = new Dialog(this); Objects.requireNonNull(webDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); webDialog.setContentView(R.layout.dialog_purchase_webview);

            WebView webView = webDialog.findViewById(R.id.webview);
            webView.getSettings().setLoadsImagesAutomatically(true);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

            String authURL = "https://www.apixyz.com/oauth/authorize?client_id=xyz&redirect_uri=http://localhost&response_type=code";
            
            webView.loadUrl(authURL);

            ImageButton webCloseBtn = webDialog.findViewById(R.id.close_btn);
            webCloseBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    webDialog.dismiss();
                }
            });

            webDialog.show();

            webView.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);

            }
            String authCode;
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                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);
                    EventCardActivity.this.setResult(Activity.RESULT_OK, resultIntent);
                    setResult(Activity.RESULT_CANCELED, resultIntent);

                    webDialog.dismiss();

                    Toast.makeText(getApplicationContext(),"Authorization Code is: " +authCode, Toast.LENGTH_SHORT).show();
                }else if(url.contains("error=access_denied")){
                    Log.i("", "ACCESS_DENIED_HERE");
                    resultIntent.putExtra("code", authCode);
                    authComplete = true;
                    setResult(Activity.RESULT_CANCELED, resultIntent);
                    Toast.makeText(getApplicationContext(), "Error Occured", Toast.LENGTH_SHORT).show();

                    webDialog.dismiss();
                }
            }
        });

无法执行该行 if (url.contains("?code=") && authComplete != true) { } 但实际上,我不知道这到底是什么意思,也不知道为什么有必要,即使我删除了它,但将 auth 设为 null。

另外,redirect_uri 应该是什么?我使用了 http://localhost/urn:ietf:wg:oauth:2.0:oob。

预期结果:应该有授权对话框来授权它,然后它应该给我一个代码。 但无法做到。

请帮忙。

最佳答案

我已经通过更新redirect_uri解决了这个问题。我搞错了。

关于android - 使用 WebView 进行 OAuth2 授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64608668/

相关文章:

java - ORMLite DISTINCT 与Where 条件

android - 在android中合并两个png文件

Android 将触摸事件传递给 WebView 的内部内容

ios - 如何修复 iOS5 和 iOS6 上有问题的 webkit-text-size-adjust?

Android - 在 SurfaceTexture 上绘制 YouTube 视频

Android Webview (Phonegap) : Disable Autocorrect, Autocapitalize 和 autocomplete 不起作用

android - Lollipop Lost WebView View Hierarchy 的 UiAutomator 转储

android - 使用 videoview 按下主页按钮后应用程序重新启动状态

java - Zbar 扫描仪错误 java.lang.RuntimeException : cancelAutoFocus failed

javascript - Android webview 拒绝执行内联脚本