栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现授权访问网页的方法

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android实现授权访问网页的方法

本文实例讲述了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;

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");
      }
      
 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);
  }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/164571.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号