diff --git a/src/testcase/twitter/OAuthHeader.java b/src/testcase/twitter/OAuthHeader.java deleted file mode 100644 index c16ea94..0000000 --- a/src/testcase/twitter/OAuthHeader.java +++ /dev/null @@ -1,114 +0,0 @@ -package testcase.twitter; - -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLEncoder; -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 params = new TreeMap(); - - public static void main(String[] args) throws Exception { - // OAuth�ɂ����ė��p����ϐ��錾 - String consumerkey = "xPk6o11catewYwxGcXvvvqo2I"; - String consumerSecret = "P9oXQstrFURXcbV8qT4OJzLjNfgYkpo1AvJzUnbywsEiDpuytb"; - String oauthToken = "E7D28QAAAAAA9Im2AAABaCZ6YNc"; - String oauthTokenSecret = "zdhnHCamrW3jLSb3GbwhVlnmJZsOqX1V"; - String method = "POST"; - String urlStr = "https://api.twitter.com/oauth/access_token"; - - // ���ʃp�����[�^�[ - SortedMap params = new TreeMap(); - params.put("oauth_consumer_key", consumerkey); - 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"); - params.put("oauth_token", oauthToken); - - // �A�N�Z�X�g�[�N���擾���ɂ̂ݗ��p����p�����[�^�[ - // �A�v���P�[�V�����̋��‚������ꍇ�ɕ\�������Ïؔԍ���ݒ肷�� - params.put("oauth_verifier", "�Ïؔԍ�"); - - { - /* - * �����ioauth_signature�j�̐��� - * ���N�G�X�g�g�[�N���擾���ƑS���������� - */ - String paramStr = ""; - for (Entry param : params.entrySet()) { - paramStr += "&" + param.getKey() + "=" + param.getValue(); - } - paramStr = paramStr.substring(1); - - String text = method + "&" + urlEncode(urlStr) + "&" - + urlEncode(paramStr); - - String key = urlEncode(consumerSecret) + "&" - + urlEncode(oauthTokenSecret); - - SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), - "HmacSHA1"); - Mac mac = Mac.getInstance(signingKey.getAlgorithm()); - mac.init(signingKey); - byte[] rawHmac = mac.doFinal(text.getBytes()); - String signature = new BASE64Encoder().encode(rawHmac); - - params.put("oauth_signature", signature); - } - - /* - * Authorization�w�b�_�̍쐬��API�̌Ăяo�� - * ���N�G�X�g�g�[�N���擾���ƑS���������� - */ - // Authorization�w�b�_�̍쐬 - String paramStr = ""; - for (Entry param : params.entrySet()) { - paramStr += ", " + param.getKey() + "=\"" - + urlEncode(param.getValue()) + "\""; - } - paramStr = paramStr.substring(2); - String authorizationHeader = "OAuth " + paramStr; - - // API�ɃA�N�Z�X - URL url = new URL(urlStr); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod(method); - connection.setRequestProperty("Authorization", authorizationHeader); - connection.connect(); - BufferedReader reader = new BufferedReader(new InputStreamReader( - connection.getInputStream())); - String response; - while ((response = reader.readLine()) != null) { - System.out.println(response); - } - } - - private static int getUnixTime() { - return (int) (System.currentTimeMillis() / 1000L); - } - - private static String urlEncode(String string) { - try { - return URLEncoder.encode(string, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } -}