嗨,我在创建Gygia签名时遇到了问题。我在this帖子上尝试了一切,我几乎可以肯定我的问题存在于我正在使用的Base64中。 这就是我现在所拥有的。
两种方法都给我错误的键
static String sign(String timestamp, String uid, String key) {
String baseString = timestamp + "_" + uid;
String lRet = "";
byte[] baseBytes;
try {
baseBytes = baseString.getBytes("UTF-8");
byte[] secretKeyBytes = org.apache.commons.codec.binary.Base64
.decodeBase64(key.getBytes());
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(new SecretKeySpec(secretKeyBytes, "HmacSHA1"));
byte[] signatureBytes = mac.doFinal(baseBytes);
byte[] encodedSign = org.apache.commons.codec.binary.Base64
.encodeBase64(signatureBytes);
lRet = new String(encodedSign, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return lRet;
}
我试过的另一个实现是这个,但我的签名包括'/'和'+'等字符,而Gigya将其踢回来。
private String constructSignature(String timestamp, String UID, String pKey) {
// Construct a "base string" for signing
String baseString = timestamp + "_" + UID;
// Convert the base string into a binary array
byte[] binaryBaseString = ConvertUTF8ToBytes(baseString);
// Convert secretKey from BASE64 to a binary array
byte[] binaryKey = ConvertFromBase64ToBytes(pKey);
// Use the HMAC-SHA1 algorithm to calculate the signature
byte[] binarySignature = hmacsha1(baseString, binaryKey);
// Convert the signature to a BASE64
String signature = ConvertToBase64(binarySignature);
return signature;
}
private byte[] ConvertUTF8ToBytes(String pString) {
try {
return pString.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
private byte[] ConvertFromBase64ToBytes(String pBase64String) {
return android.util.Base64.decode(pBase64String,
android.util.Base64.DEFAULT);
}
private String ConvertToBase64(byte[] data) {
String retString = android.util.Base64.encodeToString(data, android.util.Base64.DEFAULT);
return retString;
}
我已经上下这段代码,我使用了commons.codec Base64以及Gigya版本而没有运气。 任何指针都会被大大贬低。 此致
我使用坏密钥从Gigya返回的错误是:
errorCode:400006
errorMessage:Invalid parameter value
errorDetails:Invalid argument: invalid signature
data:{"statusCode":400,"errorMessage":"Invalid parameter
value","errorCode":400006,"callId":"0106c32c05e14afba1fc93ae0659bb69",
"errorDetails":"Invalid argument: invalid signature","statusReason":"Bad Request"}
答案 0 :(得分:0)
在阅读帖子I mentioned之后,我在接受的答案中找到SigUtils课程,基本上为你做了所有工作......花了我一段时间,我希望我不浪费任何人时间。下面是如何生成密钥:
String lSig = SigUtils.getOAuth1Signature(query+"_"+expTime, lHashedKey);
并验证:
boolean valid = SigUtils.validateUserSignature(expTime, query, lHashedKey, lSig);