尝试获取用于在Google存储设备中下载文件的签名网址是有问题的。收到SignatureDoesNotMatch错误。

private  String signString(GoogleCredential credential, String stringToSign) throws Exception {

// sign data
Signature signer = Signature.getInstance("SHA256withRSA");
signer.initSign(credential.getServiceAccountPrivateKey());
signer.update(stringToSign.getBytes("UTF-8"));
byte[] rawSignature = signer.sign();

return new String(org.apache.commons.codec.binary.Base64.encodeBase64(rawSignature, false), "UTF-8");


}

//Here is the code to get signed url
private getFileUrl(GoogleCredential credential,String bucketName, String filePath) {
    String signedParam = signString(credential, "GET\n\n\n"+expiration+"\n"+bucketName+"/"+filePath);

   // construct URL
   String url = "http://storage.googleapis.com/" + bucketName + "/" + filePath +
                "?GoogleAccessId=" + credential.getServiceAccountId() +
                "&Expires=" + expiration +
                "&Signature=" + URLEncoder.encode(signedParam, "UTF-8");
   return url;
}


我在这里想念什么吗?挣扎了一段时间。任何帮助深表感谢。

谢谢。

最佳答案

签名网址的GET路径中的存储桶名称应以“ /”开头。这解决了问题。

String signedParam = signString(credential, "GET\n\n\n"+expiration+"\n/"+bucketName+"/"+filePath);

关于java - Google Cloud已签名网址+ SignatureDoesNotMatch,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42149253/

10-12 02:40