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

所属分类: 软件编程 / Android 阅读数: 116
收藏 0 赞 0 分享

本文实例讲述了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);
  }
}
更多精彩内容其他人还在看

Android异常 java.lang.IllegalStateException解决方法

这篇文章主要介绍了Android异常 java.lang.IllegalStateException解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Split()字符串分割特殊用法案例详解

本文通过案例的形式给大家详细介绍了android中split()字符串分割特殊用法的知识,非常不错具有参考借鉴价值,感兴趣的朋友参考下
收藏 0 赞 0 分享

Android仿新浪微博启动界面或登陆界面(1)

这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿新浪微博oauth2.0授权界面实现代码(2)

这篇文章主要为大家详细介绍了Android仿新浪微博oauth2.0授权界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发中使用sqlite实现新闻收藏和取消收藏的功能

本篇文章主要介绍了sqlite实现新闻收藏和取消收藏功能,主要涉及到oracle数据库方面的内容,对于Android开发sqlite实现收藏和取消功能感兴趣的朋友可以参考下本文
收藏 0 赞 0 分享

Android仿新浪微博分页管理界面(3)

这篇文章主要为大家详细介绍了Android仿新浪微博分页管理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android UI自定义ListView实现下拉刷新和加载更多效果

这篇文章主要介绍了Android UI自定义ListView实现下拉刷新和加载更多效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android—基于微信开放平台v3SDK开发(微信支付填坑)

这篇文章主要介绍了Android—基于微信开放平台v3SDK开发(微信支付填坑),具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

Android仿新浪微博自定义ListView下拉刷新(4)

这篇文章主要为大家详细介绍了Android仿新浪微博自定义ListView下拉刷新,重点介绍了Adapter的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android控件之使用ListView实现时间轴效果

这篇文章主要介绍了Android基础控件之使用ListView实现时间轴效果的相关资料,本文是以查看物流信息为例,给大家介绍了listview时间轴的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多