本课主要讲解了在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); } }