AppEngineでGoogleCloudStorageを使用していて、POSTフォームを使用してファイルをGCSにアップロードしようとしています。私が抱えている問題は、ポリシー文書に署名するために必要な手順にあります。client_secrets.json
APIコンソールから提供された文字列であるclient_secretを簡単に取得できます。ただし、署名を作成するには、その文字列をPrivateKey
オブジェクトに変換する必要があります。私のコードは次のようになります。
//create the policy document and encode it
String policyDocument = ... //omitted for brevity
String encodedPolicy = Base64.encodeString(policyDocument);
//sign using SHA256 with RSA
String secretKey = ... //I fetch this from client_secrets.json
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initSign(secretKey); //THIS IS THE PROBLEM!
sig.update(encodedPolicy.getBytes("UTF-8"));
String signature = new String(Base64.encode(sig.sign()));
//put the values in the request attributes so we can fetch them from a JSP
req.setAttribute("policy", encodedPolicy);
req.setAttribute("signature", signature);
上記のように、私の問題はラインにあります
sig.initSign(secretKey); //THIS IS THE PROBLEM!
secretKey
文字列です。 、またはその子孫オブジェクトの1つをSignature.initSign()
期待します。の文字列を、渡すことができるPrivateKey(または派生)オブジェクトPrivateKey
に変換するにはどうすればよいですか?client_secrets.json
Signature.initSign
どんな助けでも大歓迎です。ありがとう
OK、これが私が今いるところです。以下の提案を試しましたが、すべてのドキュメントで、サービスアカウントではなく、GoogleAPIコンソールからダウンロードしたclient_secrets.jsonファイルのclient_secretを使用するように求められています。さらに、サービスアカウントではなく、ユーザーのアップロードの例を作成しようとしています。
別のページで次のコードを見つけました。
public static String signPolicyDocument(String policyDocument, String secret) {
try {
Mac mac = Mac.getInstance("HmacSHA256");
byte[] secretBytes = secret.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(secretBytes, "HmacSHA256");
mac.init(signingKey);
byte[] signedSecretBytes = mac.doFinal(policyDocument.getBytes());
return new String(Base64.encode(signedSecretBytes));
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
そして、それは私がプロセス全体を通して私を取得します...私が結果のフォームを提出するまで。次に、次の応答があります。
The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.
どのような署名方法を探していますか?