I am struggling to create signed URL to upload file to Google Storage. First, I am getting private key and clientId using JSON key:
AuthCredentials.ServiceAccountAuthCredentials serviceAccountAuthCredentials = AuthCredentials.ServiceAccountAuthCredentials.createForJson(json_resource.getInputStream());
PrivateKey key = serviceAccountAuthCredentials.credentials().getPrivateKey();
Signature signer = Signature.getInstance("SHA256withRSA");
signer.initSign(key);
After that, creating string to sign:
String upload_uri = "PUT\n\n" + expiration +
"\n/" + bucketName + "/" + folderPath + "/" + fileName;
Then signing string:
signer.update(stringToSign.getBytes("UTF-8"));
byte[] rawSignature = signer.sign();
String signature = new String(Base64.encodeBase64(rawSignature, false), "UTF-8");
And then composing URL using signed string:
final String clientId = serviceAccountAuthCredentials.account();
String url = "http://storage.googleapis.com/" +
bucketName + "/" + folderPath + "/" + fileName +
"?GoogleAccessId=" + clientId +
"&Expires=" + expiration +
"&Signature=" + URLEncoder.encode(signature, "UTF-8");
Using this URL I am getting error:
SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.GET 1485340222074 /bucketName/fileName