Newer
Older
RestfulChecker / src / testcase / twitter / OAuthHeader.java
package testcase.twitter;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Encoder;

public class OAuthHeader {
	private String consumer_key = "";
	private String consumer_secret = "";
	private String oauth_token = "";
	private String oauth_secret = "";
	private String method = "POST";
	private String url = "https://api.twitter.com/oauth/request_token";
	private SortedMap<String, String> params = new TreeMap<String, String>();

	public String getConsumer_key() {
		return consumer_key;
	}

	public void setConsumer_key(String consumer_key) {
		this.consumer_key = consumer_key;
	}

	public String getConsumer_secret() {
		return consumer_secret;
	}

	public void setConsumer_secret(String consumer_secret) {
		this.consumer_secret = consumer_secret;
	}

	public String getOauth_token() {
		return oauth_token;
	}

	public void setOauth_token(String oauth_token) {
		this.oauth_token = oauth_token;
	}

	public String getOauth_secret() {
		return oauth_secret;
	}

	public void setOauth_secret(String oauth_secret) {
		this.oauth_secret = oauth_secret;
	}
	
	public void generateSignature(){
		params.put("oauth_consumer_key", consumer_key);
		params.put("oauth_signature_method", "HMAC-SHA1");
		params.put("oauth_timestamp", String.valueOf(getUnixTime()));
		params.put("oauth_nonce", String.valueOf(Math.random()));
		params.put("oauth_version", "1.0");
		String paramStr = "";
		for (Entry<String, String> param : params.entrySet()) {
			paramStr += "&" + param.getKey() + "=" + param.getValue();
		}
		paramStr = paramStr.substring(1);

		// 署名対象テキスト(signature base string)の作成
		String text = method + "&" + urlEncode(url) + "&"
				+ urlEncode(paramStr);

		// 署名キーの作成
		String key = urlEncode(consumer_secret) + "&"
				+ urlEncode(oauth_secret);

		// HMAC-SHA1で署名を生成
		SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),
				"HmacSHA1");
		Mac mac = null;
		try {
			mac = Mac.getInstance(signingKey.getAlgorithm());
		} catch (NoSuchAlgorithmException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			mac.init(signingKey);
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		byte[] rawHmac = mac.doFinal(text.getBytes());
		String signature = new BASE64Encoder().encode(rawHmac);

		// 署名をパラメータに追加
		params.put("oauth_signature", signature);
	}
	
	public String getAuthorization() {
		String paramStr = "";
		for (Entry<String, String> param : params.entrySet()) {
			paramStr += ", " + param.getKey() + "=\""
					+ urlEncode(param.getValue()) + "\"";
		}
		paramStr = paramStr.substring(2);
		return "OAuth " + paramStr;
	}
	
	private int getUnixTime() {
		return (int) (System.currentTimeMillis() / 1000L);
	}

	private String urlEncode(String string) {
		try {
			return URLEncoder.encode(string, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}
}