strBuf.append(",oauth_timestamp=\"").append(timestamp).append("\"");
strBuf.append(",oauth_nonce=\"").append(nonce).append("\"");
strBuf.append(",oauth_signature=\"").append(sig).append("\"");
authorization = strBuf.toString();
HTTPRequest request = new HTTPRequest(url,HTTPMethod.GET);
request.addHeader(new HTTPHeader("Authorization",authorization));
URLFetchService service = URLFetchServiceFactory.getURLFetchService();
HTTPResponse response;
try {
response = service.fetch(request);
} catch (SocketTimeoutException e) {
Common.sendMessage(fromJID, "连接饭否超时,请重试");
return;
}
if(response.getResponseCode() != 200)
{
String errMsg = "出现错误,请重试";
Common.sendMessage(fromJID,errMsg);
Common.log.warning(strJID + " :" + String.valueOf(response.getResponseCode()) + ": " + new String(response.getContent()));
return;
}
/* 提取接收到的未经授权的Request Token */
String tokenstring = new String(response.getContent());
String[] tokenarr = tokenstring.split("&");
String[] tokenarr2 = tokenarr[0].split("=");
String oauth_token = tokenarr2[1];
Common.setData(fromJID,"Account","request_token",oauth_token);
/* 请求用户授权Request Token */
String strMessage = "请访问以下网址获取PIN码: \n http://fanfou.com/oauth/authorize?oauth_token="
+ oauth_token + "&oauth_callback=oob"
+ " \n 手机用户请访问: \n http://m.fanfou.com/oauth/authorize?oauth_token="
+ oauth_token + "&oauth_callback=oob"
+ " \n 然后使用\"-bind PIN码\"命令绑定账号。";
Common.sendMessage(fromJID,strMessage);
}
else if(msgarr.length == 3) //XAuth
{
URL url = new URL("http://fanfou.com/oauth/access_token");
String username = msgarr[1];
String password = msgarr[2];
params = "oauth_consumer_key=" + API.consumer_key
+ "&oauth_nonce=" + String.valueOf(nonce)
+ "&oauth_signature_method=HMAC-SHA1"
+ "&oauth_timestamp=" + String.valueOf(timestamp)
+ "&x_auth_username=" + username
+ "&x_auth_password=" + password
+ "&x_auth_mode=client_auth";
params = "GET&" + URLEncoder.encode(url.toString(),"UTF-8")
+ "&" + URLEncoder.encode(params,"UTF-8");
String sig = API.generateSignature(params);
authorization = "OAuth realm=\"Fantalker\",oauth_consumer_key=\"" + API.consumer_key
+ "\",oauth_signature_method=\"HMAC-SHA1\""
+ ",oauth_timestamp=\"" + String.valueOf(timestamp) + "\""
+ ",oauth_nonce=\"" + String.valueOf(nonce) + "\""
+ ",oauth_signature=\"" + sig + "\""
+ ",x_auth_username=\"" + username + "\""
+ ",x_auth_password=\"" + password + "\""
+ ",x_auth_mode=\"client_auth\"";
HTTPRequest request = new HTTPRequest(url,HTTPMethod.GET);
request.addHeader(new HTTPHeader("Authorization",authorization));
URLFetchService service = URLFetchServiceFactory.getURLFetchService();
HTTPResponse response;
try {
response = service.fetch(request);
} catch (SocketTimeoutException e) {