简体   繁体   English

使用 WebView 的 OAuth2 授权

[英]OAuth2 Authorization using WebView

I am working on OAuth Authorization using webview for login purpose.我正在使用 webview 进行 OAuth 授权以进行登录。 And I know this question is asked so many times but didn't get any proper example for it.我知道这个问题被问了很多次,但没有得到任何适当的例子。 So my problem is I am unable to get authCode.所以我的问题是我无法获得 authCode。 see my code :看我的代码:

final Dialog webDialog = new Dialog(this); final Dialog webDialog = new Dialog(this); Objects.requireNonNull(webDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); Objects.requireNonNull(webDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); webDialog.setContentView(R.layout.dialog_purchase_webview); 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();
                }
            }
        });

Unable to execute this line if (url.contains("?code=") && authComplete != true) { } But really I don't know what exact meaning of this and why it's necessary even I removed it but getting auth as a null.无法执行这一行 if (url.contains("?code=") && authComplete != true) { } 但我真的不知道这到底是什么意思,为什么有必要即使我删除了它但获得身份验证作为空值。

Also what should be redirect_uri?还有什么应该是redirect_uri? I have used http://localhost / urn:ietf:wg:oauth:2.0:oob.我使用过 http://localhost/urn:ietf:wg:oauth:2.0:oob。

Expected result: Should have Authorize dialog to authorize it and then it should give me a code.预期结果:应该有授权对话框来授权它,然后它应该给我一个代码。 but unable to do it.但无法做到。

please help.请帮忙。

I have solved it by updating redirect_uri.我已经通过更新redirect_uri解决了这个问题。 I had a wrong one.我错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM