Created
May 23, 2010 11:02
-
-
Save shin1ogawa/410845 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package buzz; | |
import java.util.Iterator; | |
import java.util.Map; | |
public class Main { | |
public static void main(String[] args) | |
throws java.security.InvalidKeyException, | |
java.security.NoSuchAlgorithmException, java.io.IOException { | |
if (args.length != 4) { | |
throw new IllegalStateException( | |
"cosumerKey consumerSecret accessToken accessSecret"); | |
} | |
get(args[0], args[1], args[2], args[3]); | |
} | |
private static void get(String consumerKey, String consumerSecret, | |
String accessKey, String accessSecret) throws java.io.IOException, | |
java.security.InvalidKeyException, java.security.NoSuchAlgorithmException { | |
String method = "GET"; | |
String urlString = "https://www.googleapis.com/buzz/v1/activities/@me/@self"; | |
java.net.URL url = new java.net.URL(urlString); | |
java.net.HttpURLConnection con = (java.net.HttpURLConnection) url | |
.openConnection(); | |
con.setRequestMethod(method); | |
String authorization = authorizationHeaderValue(method, urlString, | |
consumerKey, consumerSecret, accessKey, accessSecret).toString(); | |
System.out.println("authorization=" + authorization); | |
con.addRequestProperty("Authorization", authorization); | |
con.connect(); | |
log(con); | |
} | |
private static void log(java.net.HttpURLConnection con) | |
throws java.io.IOException { | |
System.out.println(con.getResponseCode()); | |
java.io.BufferedReader in = null; | |
try { | |
in = new java.io.BufferedReader(new java.io.InputStreamReader(con | |
.getInputStream())); | |
} catch (java.io.IOException ex) { | |
System.out.println(String.format("IOException: [%d]%s", con | |
.getResponseCode(), con.getResponseMessage())); | |
throw ex; | |
} finally { | |
if (in != null) { | |
String line; | |
while ((line = in.readLine()) != null) { | |
System.out.println(line); | |
} | |
in.close(); | |
} | |
} | |
} | |
private static StringBuilder authorizationHeaderValue(String method, | |
String url, String consumerKey, String consumerSecret, String accessKey, | |
String accessSecret) throws java.io.UnsupportedEncodingException, | |
java.security.InvalidKeyException, java.security.NoSuchAlgorithmException { | |
Map<String, String> authParams = new java.util.LinkedHashMap<String, String>(); | |
authParams.put("oauth_nonce", String.valueOf((long) (Math.random() * Math | |
.pow(2.0, 64.0)))); | |
authParams.put("oauth_signature_method", "HMAC-SHA1"); | |
authParams.put("oauth_consumer_key", consumerKey); | |
authParams.put("oauth_token", accessKey); | |
authParams.put("oauth_timestamp", String | |
.valueOf(System.currentTimeMillis() / 1000)); | |
String baseString = generateSignatureBaseString(method, url, authParams); | |
System.out.println("baseString=" + baseString); | |
String signature = generateOauthSignature(baseString, consumerSecret, | |
accessSecret); | |
Map<String, String> authParams2 = new java.util.LinkedHashMap<String, String>(); | |
authParams2.put("oauth_signature", signature); | |
authParams2.putAll(authParams); | |
StringBuilder b = new StringBuilder("OAuth "); | |
boolean first = true; | |
Iterator<java.util.Map.Entry<String, String>> i = authParams2.entrySet() | |
.iterator(); | |
while (i.hasNext()) { | |
if (first == false) { | |
b.append(", "); | |
} else { | |
first = false; | |
} | |
java.util.Map.Entry<String, String> next = i.next(); | |
b.append(next.getKey()).append("=\"").append(next.getValue()) | |
.append("\""); | |
} | |
return b; | |
} | |
// | |
static final String ENC = "utf-8"; | |
static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; | |
static String generateSignatureBaseString(String method, | |
String requestUrlBase, java.util.Map<String, String> params) | |
throws java.io.UnsupportedEncodingException { | |
return escapeAndJoin(java.util.Arrays.asList(method, requestUrlBase, | |
formatUrlParams(params))); | |
} | |
static String escapeAndJoin(Iterable<String> params) | |
throws java.io.UnsupportedEncodingException { | |
StringBuilder b = new StringBuilder(); | |
boolean first = true; | |
for (String s : params) { | |
if (first == false) { | |
b.append('&'); | |
} else { | |
first = false; | |
} | |
b.append(java.net.URLEncoder.encode(s, ENC)); | |
} | |
return b.toString(); | |
} | |
static String formatUrlParams(java.util.Map<String, String> params) | |
throws java.io.UnsupportedEncodingException { | |
StringBuilder b = new StringBuilder(); | |
boolean first = true; | |
for (String key : sorted(params)) { | |
if (first == false) { | |
b.append('&'); | |
} else { | |
first = false; | |
} | |
b.append(key).append('=').append( | |
java.net.URLEncoder.encode(params.get(key), ENC).replace("+", "%20")); | |
} | |
return b.toString(); | |
} | |
static java.util.List<String> sorted(java.util.Map<String, String> map) { | |
java.util.List<String> list = new java.util.ArrayList<String>(map.keySet()); | |
java.util.Collections.sort(list); | |
return list; | |
} | |
static String generateOauthSignature(String baseString, | |
String consumerSecret, String tokenSecret) | |
throws java.security.InvalidKeyException, | |
java.security.NoSuchAlgorithmException, | |
java.io.UnsupportedEncodingException { | |
return java.net.URLEncoder.encode(generateHmacSha1Signature(baseString, | |
escapeAndJoin(java.util.Arrays.asList(consumerSecret, tokenSecret))), | |
ENC); | |
} | |
static String generateHmacSha1Signature(String data, String key) | |
throws java.security.NoSuchAlgorithmException, | |
java.security.InvalidKeyException { | |
javax.crypto.spec.SecretKeySpec signingKey = new javax.crypto.spec.SecretKeySpec( | |
key.getBytes(), HMAC_SHA1_ALGORITHM); | |
javax.crypto.Mac mac = javax.crypto.Mac.getInstance(HMAC_SHA1_ALGORITHM); | |
mac.init(signingKey); | |
return byteArrayToBase64(mac.doFinal(data.getBytes())); | |
} | |
static String byteArrayToBase64(byte[] bytes) { | |
int aLen = bytes.length; | |
int numFullGroups = aLen / 3; | |
int numBytesInPartialGroup = aLen - 3 * numFullGroups; | |
int resultLen = 4 * ((aLen + 2) / 3); | |
StringBuilder b = new StringBuilder(resultLen); | |
int inCursor = 0; | |
for (int i = 0; i < numFullGroups; i++) { | |
int byte0 = bytes[inCursor++] & 0xff; | |
int byte1 = bytes[inCursor++] & 0xff; | |
int byte2 = bytes[inCursor++] & 0xff; | |
b.append(intToBase64[byte0 >> 2]); | |
b.append(intToBase64[(byte0 << 4) & 0x3f | (byte1 >> 4)]); | |
b.append(intToBase64[(byte1 << 2) & 0x3f | (byte2 >> 6)]); | |
b.append(intToBase64[byte2 & 0x3f]); | |
} | |
if (numBytesInPartialGroup != 0) { | |
int byte0 = bytes[inCursor++] & 0xff; | |
b.append(intToBase64[byte0 >> 2]); | |
if (numBytesInPartialGroup == 1) { | |
b.append(intToBase64[(byte0 << 4) & 0x3f]); | |
b.append("=="); | |
} else { | |
int byte1 = bytes[inCursor++] & 0xff; | |
b.append(intToBase64[(byte0 << 4) & 0x3f | (byte1 >> 4)]); | |
b.append(intToBase64[(byte1 << 2) & 0x3f]); | |
b.append('='); | |
} | |
} | |
return b.toString(); | |
} | |
static final char intToBase64[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | |
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', | |
'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', | |
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', | |
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment