![](/img/trans.png)
[英]Using Android AccountManager for 3rd party OAuth2 authorization
[英]OAuth2 Authorization using WebView
我正在使用 webview 进行 OAuth 授权以进行登录。 我知道这个问题被问了很多次,但没有得到任何适当的例子。 所以我的问题是我无法获得 authCode。 看我的代码:
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();
}
}
});
无法执行这一行 if (url.contains("?code=") && authComplete != true) { } 但我真的不知道这到底是什么意思,为什么有必要即使我删除了它但获得身份验证作为空值。
还有什么应该是redirect_uri? 我使用过 http://localhost/urn:ietf:wg:oauth:2.0:oob。
预期结果:应该有授权对话框来授权它,然后它应该给我一个代码。 但无法做到。
请帮忙。
我已经通过更新redirect_uri解决了这个问题。 我错了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.