首先,创建Oauth类,以备后用。
/**
*
* @author mrlixirong
*
* 2011-11-16
*/
public class Oauth {
//此处一律设置成public只为方便使用
public String oauth_consumer_key=null;
public String oauth_consumer_secret=null;
public String oauth_nonce=null;
public String oauth_timestamp=null;
public String oauth_signature_method=null;
public String oauth_signature=null;
public String oauth_token=null;
public String oauth_token_secret=null;
public String oauth_version=null;
public String oauth_callback=null;
public String oauth_verifier=null;
public boolean status=false;//该标志与oauth内容无关,此处留用作为授权成功与否标志
public Oauth(String key,String secret){
this.oauth_callback="null";
this.oauth_version="1.0";
this.oauth_signature_method="HMAC-SHA1";
this.oauth_consumer_key=key;
this.oauth_consumer_secret=secret;
}
}
创建OauthClient类,包含reqeustToken()和accessToken()方法
/**
*
* @author mrlixirong
*
* 2011-11-16
*/
public class OauthClient {
/*
* 此处传入参数 Context context是用于获取网路连接管理器时使用
*/
public static Oauth requestToken(Oauth oauth,Context context){
oauth.status=false;
oauth.oauth_nonce=Utils.getRandomString(32);
oauth.oauth_timestamp=String.valueOf(System.currentTimeMillis()/1000);
BaseString bs=new BaseString();
bs.setHttpMethod("GET");
bs.setURL("http://open.t.qq.com/cgi-bin/request_token");
bs.addParams("oauth_consumer_key", oauth.oauth_consumer_key);
bs.addParams("oauth_signature_method", oauth.oauth_signature_method);
bs.addParams("oauth_callback", oauth.oauth_callback);
bs.addParams("oauth_version", oauth.oauth_version);
bs.addParams("oauth_timestamp", oauth.oauth_timestamp);
bs.addParams("oauth_nonce", oauth.oauth_nonce);
oauth.oauth_signature=Utils.getSignature(bs.getBaseString(),oauth.oauth_consumer_secret+"&");
//
String params=null;
try {
params = "oauth_consumer_key="+URLEncoder.encode(oauth.oauth_consumer_key,"UTF-8")+
"&oauth_signature_method="+URLEncoder.encode(oauth.oauth_signature_method,"UTF-8")+
"&oauth_timestamp="+URLEncoder.encode(oauth.oauth_timestamp,"UTF-8")+
"&oauth_nonce="+URLEncoder.encode(oauth.oauth_nonce,"UTF-8")+
"&oauth_version="+URLEncoder.encode(oauth.oauth_version,"UTF-8")+
"&oauth_signature="+URLEncoder.encode(oauth.oauth_signature,"UTF-8")+
"&oauth_callback="+URLEncoder.encode(oauth.oauth_callback,"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
//
try {
HttpURLConnection con =null;
URL postUrl = new URL("http://open.t.qq.com/cgi-bin/request_token?"+params);
/*
* 实际联网中,作为Android手机应用,我们必须根据其网络类型判断是否需要设置代理
* 此处只针对是否wifi做出处理,如果不是wifi,默认为2G/3G网络
*
boolean isProxy=false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm!=null){
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni!=null){
if(! ni.getTypeName().equals("WIFI")){
isProxy=true;//如果不是wifi,则需要设置代理
}
}
}
if(isProxy){
Sting host=android.net.Proxy.getDefaultHost();//得到默认代理地址,中国移动gprs用户返回10.0.0.172
int port=android.net.Proxy.getDefaultPort();//得到默认代理端口,中国移动gprs用户返回80
SocketAddress sa=new InetSocketAddress(host,port);
Proxy proxy=new Proxy(java.net.Proxy.Type.HTTP,sa);
con = (HttpURLConnection) postUrl.openConnection(proxy);
}
*/
con =(HttpURLConnection) postUrl.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(30000);
con.setReadTimeout(20000);
con.setDoInput(true);
int code = con.getResponseCode();
if(code==200){
InputStream is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer buf = new StringBuffer();
String line;
while (null != (line = br.readLine())) {
buf.append(line).append("\n");
}
/*
* 正确的响应结果是:oauth_token=xxx&oauth_token_secret=xxx&oauth_callback_confirmed=true
*/
String result=buf.toString();
if(result.contains("oauth_callback_confirmed=true")){
int len=result.length();
int i=0;
while(result.charAt(i)!='&' && i<len){
i++;
}
oauth.oauth_token=result.substring(12,i);
int j=i+1;
while(result.charAt(j)!='&' && j<len){
j++;
}
oauth.oauth_token_secret=result.substring(i+20,j);
oauth.status=true;//授权成功(ps:此处判断方式不够完善,仅作测试使用)
}
}
} catch (IOException e) {
e.printStackTrace();
}
return oauth;
}
/*
* 此处传入参数 Context context是用于获取网路连接管理器时使用
*/
public static Oauth accessToken(Oauth oauth,Context context){
oauth.status=false;
oauth.oauth_nonce=Utils.getRandomString(32);
oauth.oauth_timestamp=String.valueOf(System.currentTimeMillis()/1000);
BaseString bs=new BaseString();
bs.setHttpMethod("GET");
bs.setURL("http://open.t.qq.com/cgi-bin/access_token");
bs.addParams("oauth_consumer_key", oauth.oauth_consumer_key);
bs.addParams("oauth_nonce", oauth.oauth_nonce);
bs.addParams("oauth_signature_method", oauth.oauth_signature_method);
bs.addParams("oauth_timestamp", oauth.oauth_timestamp);
bs.addParams("oauth_token", oauth.oauth_token);
bs.addParams("oauth_verifier",oauth.oauth_verifier);
bs.addParams("oauth_version", oauth.oauth_version);
oauth.oauth_signature=Utils.getSignature(bs.getBaseString(),oauth.oauth_consumer_secret+"&"+oauth.oauth_token_secret);
//
String params="";
try {
params = "oauth_consumer_key="+URLEncoder.encode(oauth.oauth_consumer_key,"UTF-8")+
"&oauth_token="+URLEncoder.encode(oauth.oauth_token,"UTF-8")+
"&oauth_signature_method="+URLEncoder.encode(oauth.oauth_signature_method,"UTF-8")+
"&oauth_timestamp="+URLEncoder.encode(oauth.oauth_timestamp,"UTF-8")+
"&oauth_nonce="+URLEncoder.encode(oauth.oauth_nonce,"UTF-8")+
"&oauth_version="+URLEncoder.encode(oauth.oauth_version,"UTF-8")+
"&oauth_signature="+URLEncoder.encode(oauth.oauth_signature,"UTF-8")+
"&oauth_verifier="+URLEncoder.encode(oauth.oauth_verifier,"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
//
try {
HttpURLConnection con = null;
URL getUrl = new URL("http://open.t.qq.com/cgi-bin/access_token?"+params);
/*
* 此处在实际中也要像获取request_token一样判断网络类型设置代理
*/
con = (HttpURLConnection) getUrl.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(30000);
con.setReadTimeout(20000);
con.setDoInput(true);
int code = con.getResponseCode();
if(code==200){
InputStream is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer buf = new StringBuffer();
String line;
while (null != (line = br.readLine())) {
buf.append(line).append("\n");
}
String result=buf.toString();
/*
* 正确的响应结果是:oauth_token=xxx&oauth_token_secret=xxx&name=xxx
*/
if(result.contains("oauth_token") && result.contains("name")){
int len=result.length();
int i=0;
while(result.charAt(i)!='&' && i<len){
i++;
}
oauth.oauth_token=result.substring(12,i);
int j=i+1;
while(result.charAt(j)!='&' && j<len){
j++;
}
oauth.oauth_token_secret=result.substring(i+20,j);
oauth.status=true;//授权成功(ps:此处判断方式不够完善,仅作测试使用)
}
}
} catch (IOException e) {
e.printStackTrace();
}
return oauth;
}
}
项目源代码:http://download.csdn.net/detail/mrlixirong/3804385