import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
public class Proxy {
static String consumerKey = "shiyou-top.blogspot.com";
static String consumerSecret = "E3LSNU04y5wgfOxLbDfmy7JY";
public static void requestToken(){
String requestTokenUrl = "https://www.google.com/accounts/OAuthGetRequestToken?oauth_callback=oob&scope=https://www.googleapis.com/auth/buzz";
requestTokenUrl = signRequest(requestTokenUrl);
System.out.println(requestTokenUrl);
try {
System.out.println(getUrlData(requestTokenUrl));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String getUrlData(String url) throws IOException{
URL myUrl = new URL(url);
//取得 URLConnection
HttpURLConnection conn = (HttpURLConnection)myUrl.openConnection();
System.err.println(conn);
conn.setRequestMethod("GET");
conn.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((reader.readLine()) != null){
System.out.println(reader.readLine()); //印出結果
}
return reader.readLine();
}
/*
* Sign request method
*/
//httpmethod is "get", without oauth_token_secret
private static String signRequest(String url){
/*
* adding variables which used in url every time
*/
url += "&oauth_consumer_key=" + consumerKey ;
url += "&oauth_signature_method=HMAC-SHA1";
int timestamp = Math.round(new Date().getTime() / 1000);
url += "&oauth_timestamp=" + timestamp;
url += "&oauth_nonce=" + (int)(Math.random() * 34 + 10);
url += "&oauth_version=1.0";
//split url
String urlVariables = url.substring(url.indexOf("?") + 1);
String urlVar[] = urlVariables.split("&");
Arrays.sort(urlVar);
//set stringToSign
String sortedUrl = "";
for(int i = 0; i < urlVar.length; i++){
//if there is an url inside the hole url, encode it.
if(urlVar[i].indexOf("://") > 0){
String splitUrlVar[] = urlVar[i].split("=");
try {
splitUrlVar[1] = URLEncoder.encode(splitUrlVar[1], "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
urlVar[i] = splitUrlVar[0] + "=" + splitUrlVar[1];
}
sortedUrl += urlVar[i];
if(i != (urlVar.length - 1)){
sortedUrl += "&";
}
}
url = url.substring(0,url.indexOf("?")) + "?" +sortedUrl;
try {
sortedUrl = URLEncoder.encode(sortedUrl,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String encodeURL = null;
try {
encodeURL = URLEncoder.encode(url.substring(0,url.indexOf("?")),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String stringToSign = "GET" + "&" + encodeURL + "&" + sortedUrl;
String secret = consumerSecret + "&";
String sign = hmac_sha1(stringToSign, secret);
return url + "&oauth_signature=" + sign;
}
private static String hmac_sha1(String data, String key){
byte[] b = HMACSHA1.getHmacSHA1(data, key);
String s = new BASE64Encoder().encode(b);
//System.out.println("hmacsha1 = " + s);
return s;
}
public static void main(String[] args){
requestToken();
}
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言