// 生成oauth_signature public String generateSignature(String data, Token token) throws WeiboException { byte[] byteHMAC = null; try { Mac mac = Mac.getInstance(HttpHeaderFactory.CONST_HMAC_SHA1); SecretKeySpec spec = null; String oauthSignature = encode(Weibo.getAppSecret()) + "&"; spec = new SecretKeySpec(oauthSignature.getBytes(), HttpHeaderFactory.CONST_HMAC_SHA1); mac.init(spec); byteHMAC = mac.doFinal(data.getBytes()); } catch (InvalidKeyException e) { throw new WeiboException(e); } catch (NoSuchAlgorithmException e) { throw new WeiboException(e); } return String.valueOf(Utility.base64Encode(byteHMAC)); }
@Override public String generateSignature(String data, Token token) { byte[] byteHMAC = null; try { Mac mac = Mac.getInstance(HttpHeaderFactory.CONST_HMAC_SHA1); SecretKeySpec spec = null; if (null == token.getSecretKeySpec()) { String oauthSignature = encode(Weibo.getAppSecret()) + "&" + encode(token.getSecret()); spec = new SecretKeySpec(oauthSignature.getBytes(), HttpHeaderFactory.CONST_HMAC_SHA1); token.setSecretKeySpec(spec); } spec = token.getSecretKeySpec(); mac.init(spec); byteHMAC = mac.doFinal(data.getBytes()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } return String.valueOf(Utility.base64Encode(byteHMAC)); }