亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

新浪微博Android客戶端學習記錄四:完成OAuth認

系統 1818 0

本課主要講解了在Android 中如何完成用戶OAuth認證。

微博操作的流程圖:

新浪微博Android客戶端學習記錄四:完成OAuth認證

點擊了開始后:

新浪微博Android客戶端學習記錄四:完成OAuth認證

首先是獲取新浪微博的OAuth授權(WebViewActivity.java):

    package haiyang.project.iweibo.ui;

import haiyang.project.iweibo.R;
import haiyang.project.iweibo.util.AuthUtil;
import haiyang.project.iweibo.util.JavascriptInterface;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

/*
 * 新浪微博OAuth授權頁面
 */
public class WebViewActivity extends Activity{
	
	private WebView webView;
	private ProgressDialog progressDialog;
	private static final int CLOSE_DLG=1;
	private Handler handler;
	String url=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.webview);
		
		url=AuthUtil.getAuthorizationURL();
		if(url==null){
			Toast.makeText(this, R.string.auth_url_empty, 3000).show();
			return;
		}
		
		init();
		load(url,webView);
		
		handler=new Handler(){
			public void handleMessage(android.os.Message msg) {
				if(msg.what==CLOSE_DLG){
					progressDialog.dismiss();
				}
			};
		};
	}
	
	public void init(){
		if(progressDialog==null){
			progressDialog=new ProgressDialog(this);
		}
		progressDialog.setMessage("正在加載");
		progressDialog.show();
		
		webView=(WebView) this.findViewById(R.id.wv_oauth); 
		webView.getSettings().setJavaScriptEnabled(true);
		webView.addJavascriptInterface(new JavascriptInterface(), "Methods");
		
		webView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				load(url,webView);
				return true;
			}
			
			@Override
			public void onPageFinished(WebView view, String url) {
				if(url.equals("http://api.t.sina.com.cn/oauth/authorize")){
					String load="javascript:window.Methods.getPin('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>')";
					view.loadUrl(load);
					
					Intent intent=new Intent(WebViewActivity.this,AccessTokenActivity.class);
					startActivity(intent);
				}
				super.onPageFinished(view, url);
			}
			
		});
		
		webView.setWebChromeClient(new WebChromeClient(){

			public void onProgressChanged(WebView view, int newProgress) {
				if(newProgress==100){
					handler.sendEmptyMessage(CLOSE_DLG);
				}
				super.onProgressChanged(view, newProgress);
			}
		});
	}
	
	public void load(final String url,final WebView view){
		if(url==null||"".equals(url)){
			return;
		}
		new Thread(){
			public void run() {
				view.loadUrl(url);
			}
		}.start();
	}
}

  

獲取授權URL(AuthUtil.java)

    package haiyang.project.iweibo.util;

import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;

public class AuthUtil {
	
	private static Weibo weibo;
	private static RequestToken requestToken;
	static{
		System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
		weibo=new Weibo();
	}
	
	/**
	 * 獲取授權URL
	 * @return
	 */
	public static String getAuthorizationURL(){
		try {
			requestToken=weibo.getOAuthRequestToken();
			return requestToken.getAuthorizationURL();
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static void getAccessToken(String pin){
		try {
			AccessToken accessToken=requestToken.getAccessToken(pin);
			System.out.println("Access token: "+ accessToken.getToken());
			System.out.println("Access token secret: "+ accessToken.getTokenSecret());
			System.out.println("userID: "+accessToken.getUserId());
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

  

獲取PIN值

    package haiyang.project.iweibo.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 獲取PIN值
 * @author Administrator
 *
 */
public class JavascriptInterface {
	
	//授權碼
	public static String PIN=null;

	public void getPin(String html){

		String pin=null;
		String reg="[0-9]{6}";
		
		Pattern pattern=Pattern.compile(reg);
		Matcher matcher=pattern.matcher(html);
	
		if(matcher.find()){//find each match in turn
			pin=matcher.group(0);//Access a submatch group
		}
		PIN=pin;
	}
}

  

獲取AccessToken

    package haiyang.project.iweibo.ui;

import haiyang.project.iweibo.util.AuthUtil;
import haiyang.project.iweibo.util.JavascriptInterface;
import android.app.Activity;
import android.os.Bundle;

public class AccessTokenActivity extends Activity{
	protected void onCreate(Bundle savedInstanceState) {
		
		String pin=JavascriptInterface.PIN;
		AuthUtil.getAccessToken(pin);
		super.onCreate(savedInstanceState);
	}
}

  

新浪微博Android客戶端學習記錄四:完成OAuth認證


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久国产偷 | 日日碰| 99免费在线视频 | 欧美日韩日本国产 | 亚洲欧洲国产精品久久 | 亚洲六月丁香婷婷综合 | 国产亚洲欧美日韩国产片 | 伊人激情久久综合中文字幕 | 欧美深夜影院 | 9966久久精品免费看国产 | 欧美一级高清视频在线播放 | 日本免费的一级绿象 | 麻豆视频一区 | 亚洲精品中文字幕久久久久久 | 色综合久久综精品 | 国产精品国色综合久久 | 欧美一级毛片免费看 | 色吧五月婷婷 | 夜夜狠狠狠狠 | 免费视频成人国产精品网站 | 一级毛片免费视频 | 一级毛片特黄久久免费看 | 国产精品手机视频 | 一区二区视频免费看 | 91精品国产综合久久久久久 | 国产成人精品一区二三区在线观看 | 三级aa久久 | 欧美日韩视频一区二区 | 日本一级在线播放线观看免 | 婷婷在线免费观看 | 亚洲精彩视频在线观看 | 亚洲国产欧美日韩 | 国产精品视频观看 | 亚洲综合在线另类色区奇米 | 草草影院国产第一页 | 欧美一级视频在线 | 乱人伦中文字幕在线看 | 国产成人在线视频播放 | 久久精品国内偷自一区 | 色综合久久久高清综合久久久 | 亚洲乱码一区二区三区在线观看 |