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.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.oauthv2.OAuthV2Client;
/**
 *  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 android.app.Activity#onActivityResult(int 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;
  @SuppressLint("NewApi")
 @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview_qq);
    mWebView = (WebView) findViewById(R.id.qq_mywebview);;
    mWebView.setVerticalScrollBarEnabled(false);
 mWebView.setHorizontalScrollBarEnabled(false);
 Intent intent = this.getIntent();
    oAuth = (OAuthV2) intent.getExtras().getSerializable("oauth");
    String urlStr = OAuthV2Client.generateImplicitGrantUrl(oAuth);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(true);
    mWebView.requestFocus();
    mWebView.loadUrl(urlStr);
    System.out.println(urlStr.toString());
    Log.i(TAG, "WebView Starting....");
    WebViewClient client = new WebViewClient() {
    /*  Callback method, executed when the page loads */
      @Override
      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);
          finish();
        }
        super.onPageStarted(view, url, favicon);
        Log.i(TAG, "999999999");
      }
      /* TODO Android2.2 And above versions are available to use this method currently https://open.t.qq.com 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("https://open.t.qq.com"))) {
          handler.proceed();//  Accept the certificate 
        } else {
          handler.cancel(); //  By default, WebView A blank 
        }
        // handleMessage(Message msg);  Other processing 
      }
    };
    mWebView.setWebViewClient(client);
  }
}

Related articles: