2017-02-10 91 views
1

尝试获取用于在Google存储中下载文件的签名网址存在问题。获取SignatureDoesNotMatch错误。Google Cloud signed Url + 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; 
} 

我错过了什么吗?为此挣扎了一段时间。任何帮助深表感谢。

感谢。

回答

0

签名url的GET路径中的存储区名称应该以“/”开头。这解决了这个问题。

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