Reputation: 328
I am working on OAuth Authorization using webview for login purpose. 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. see my code :
final Dialog 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();
}
}
});
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.
Also what should be redirect_uri? I have used 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.
Upvotes: 2
Views: 1402