Android實現授權訪問網頁的方法_Android

來源:互聯網
上載者:User

本文執行個體講述了Android授權訪問網頁的實現方法,即使用Webview顯示OAuth Version 2.a ImplicitGrant方式授權的頁,但是對於移動終端不建議使用Authorize code grant方式授權

具體功能代碼如下所示:

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;/** * 使用Webview顯示OAuth Version 2.a ImplicitGrant方式授權的頁 * (移動終端不建議使用Authorize code grant方式授權 * 本類使用方法 * 調用本類的地方請添加如下代碼 * //請將OAuthV2Activity改為類的類名 * Intent intent = new Intent(OAuthV2Activity.this, OAuthV2AuthorizeWebView.class); * intent.putExtra("oauth", oAuth); //oAuth為OAuthV2類的執行個體,存放授權相關信?? * startActivityForResult(intent, myRrequestCode); //請設定合適的requsetCode * 重寫接收回調資訊的方 * if (requestCode==myRrequestCode) { //對應之前設定的的myRequsetCode *   if (resultCode==OAuthV2AuthorizeWebView.RESULT_CODE) { *     //取得返回的OAuthV2類執行個體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() {    /* 回調方法,當頁面載入時執行*/      @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及以上版本才能使用該方法,目前https://open.t.qq.com中存在http資源會引起sslerror,待網站修正後可去掉該方*/ public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {        if ((null != view.getUrl()) && (view.getUrl().startsWith("https://open.t.qq.com"))) {          handler.proceed();// 接受認證        } else {          handler.cancel(); // 預設的處理方式,WebView變成空白        }        // handleMessage(Message msg); 其他處理      }    };    mWebView.setWebViewClient(client);  }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.