Android implements methods to authorize access to web pages

  • 2020-06-03 08:17:53
  • OfStack

This article illustrates the implementation method of Android authorization to access web pages, that is, Webview is used to display OAuth Version 2.ES5en ImplicitGrant page authorization, but Authorize code grant authorization is not recommended for mobile terminals.

The specific function code is as follows:

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
 *  use Webview According to OAuth Version 2.a ImplicitGrant Method to authorize the page 
 * ( Mobile terminals are not recommended Authorize code grant Way to authorize 
 *  Use method of this class 
 *  Add the following code where this class is called 
 * // Please send OAuthV2Activity Change the class name of the class 
 * Intent intent = new Intent(OAuthV2Activity.this, OAuthV2AuthorizeWebView.class);
 * intent.putExtra("oauth", oAuth); //oAuth for OAuthV2 Class to store authorization letters ??
 * startActivityForResult(intent, myRrequestCode); // Please set it appropriately requsetCode
 *  Overrides the party that receives the callback information 
 * if (requestCode==myRrequestCode) { // That corresponds to what we set up before myRequsetCode
 *   if (resultCode==OAuthV2AuthorizeWebView.RESULT_CODE) {
 *     // Get the returned OAuthV2 The class instance oAuth
 *     oAuth=(OAuthV2) data.getExtras().getSerializable("oauth");
 *   }
 * }
 * @see requestCode, int resultCode, Intent data)
public class MyWebView extends Activity {
  public final static int RESULT_CODE = 2;
  private OAuthV2 oAuth;
  private final String TAG = "MyWebView";
 private WebView mWebView;
  public void onCreate(Bundle savedInstanceState) {
    mWebView = (WebView) findViewById(;;
 Intent intent = this.getIntent();
    oAuth = (OAuthV2) intent.getExtras().getSerializable("oauth");
    String urlStr = OAuthV2Client.generateImplicitGrantUrl(oAuth);
    WebSettings webSettings = mWebView.getSettings();
    Log.i(TAG, "WebView Starting....");
    WebViewClient client = new WebViewClient() {
    /*  Callback method, executed when the page loads */
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
        Log.i(TAG, "WebView onPageStarted...");
        Log.i(TAG, "URL = " + url);
        if (url.indexOf("access_token=") != -1) {
          int start=url.indexOf("access_token=");
          String responseData=url.substring(start);
          OAuthV2Client.parseAccessTokenAndOpenId(responseData, oAuth);
          Intent intent = new Intent();
          intent.putExtra("oauth", oAuth);
          setResult(RESULT_CODE, intent);
        super.onPageStarted(view, url, favicon);
        Log.i(TAG, "999999999");
      /* TODO Android2.2 And above versions are available to use this method currently in http Resources can cause sslerror , the party can be removed after the website is corrected */
 public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        if ((null != view.getUrl()) && (view.getUrl().startsWith(""))) {
          handler.proceed();//  Accept the certificate 
        } else {
          handler.cancel(); //  By default, WebView A blank 
        // handleMessage(Message msg);  Other processing 

Related articles: