$params = array(
'appid' => $appid,
'apikey' => $apikey,
'secretkey' => $secretkey,
'timestamp' => $timestamp,
);
你这个最后其实应该是多一个逗号,会默认添加一个&符号吧。
'appid' => $appid,
'apikey' => $apikey,
'secretkey' => $secretkey,
'timestamp' => $timestamp,
);
你这个最后其实应该是多一个逗号,会默认添加一个&符号吧。
String apikey = "b29de5dc-d92d-de8b-ce32-8292-855s9c9a";
String secretkey = "TgktZRkt0upf/oYtegyblm4XTUuEFMgCuskOrB29FfQNwSKjGosQeUQtuLU/";
String timestamp = "1402484607"; String url = "apikey=" + apikey + "&appid=" + appid + "&secretkey=" + URLEncoder.encode(secretkey, "UTF-8") + "×tamp=" + timestamp; SecretKeySpec key = new SecretKeySpec(secretkey.getBytes("UTF-8"), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(key);
mac.update(url.getBytes("UTF-8")); byte[] rawHmac = mac.doFinal(); StringBuilder builder = new StringBuilder(rawHmac.length);
for (byte b : rawHmac) {
int i = b & 0xff;
if (i <= 0xf) {
builder.append("0");
}
builder.append(Integer.toHexString(i));
}
String macStr = builder.toString();//这个结果对应hash_hmac的结果,注意php返回的是hex字符串
//php base64_encode的参数是个字符串,java的是个byte[],你的错误就出在这里,不能直接把mac数组进行编码,要先转成hexstr后再进行编码
System.out.println(new BASE64Encoder().encode(macStr.getBytes("UTF-8")));