gpt4 book ai didi

java - Google 云存储桶 - 上传文件 - 403 Forbidden Android SignatureDoesNotMatch

转载 作者:行者123 更新时间:2023-12-01 21:48:22 25 4
gpt4 key购买 nike

因此,请遵循此处的 GCSSignedURLExample.java 示例

https://cloud.google.com/storage/docs/access-control#Construct-the-String

我在eclipse中尝试了代码。我的 MacBook 上的简单 .java 文件程序。而且效果很好。当我登录 console.developer.google.com 并浏览存储桶内容时,我可以看到创建的文本文件。

但是我尝试从 Android 执行同一段代码。我收到 403错误。

因此,当从 Mac 上的 .java 程序运行时,下面的代码可以正常工作。但在 android 中执行时,它给出 403。

任何帮助将不胜感激。如果需要,我可以添加更多代码。

        key = loadKeyFromPkcs12(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret".toCharArray());

System.out.println("======= PUT File =========");
String put_url = this.getSigningURL("PUT");

URL url = new URL(put_url);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
out.write("Lorem ipsum");
out.close();

System.out.println("PUT Request URL: " + put_url);
System.out.println("PUT Response code: " + httpCon.getResponseCode());
renderResponse(httpCon.getInputStream());

编辑

好吧,再调试 403 我发现它是一个 SignatureDoesNotMatch

<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided.
Check your Google secret key and signing method.
</Message>

<StringToSign>PUT
application/x-www-form-urlencoded
1456322965
/my-bucket/234/somerandomfile.txt
</StringToSign>

我仍然无法弄清楚这一点。这是签名代码。这与我在 eclipse java 代码上所做的相同(工作代码)

private String getSigningURL(String verb) throws Exception {
String url_signature = this.signString(verb + "\n\n\n" + expiration + "\n" + "/" + BUCKET_NAME + "/" + OBJECT_NAME );
String signed_url = "https://storage.googleapis.com/" + BUCKET_NAME + "/" + OBJECT_NAME +
"?GoogleAccessId=" + SERVICE_ACCOUNT_EMAIL +
"&Expires=" + expiration +
"&Signature=" + URLEncoder.encode(url_signature, "UTF-8");
return signed_url;
}

private PrivateKey loadKeyFromPkcs12(String filename, char[] password) throws Exception {

KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(getAssets().open(filename), password);
return (PrivateKey) ks.getKey("privatekey", password);
}

private String signString(String stringToSign) throws Exception {
if (key == null)
throw new Exception("Private Key not initalized");
Signature signer = Signature.getInstance("SHA256withRSA");
signer.initSign(key);
signer.update(stringToSign.getBytes("UTF-8"));
byte[] rawSignature = signer.sign();
return new String(Base64.encodeBase64(rawSignature, false), "UTF-8");
}

最佳答案

终于解决了。没有直接的文档引导我来到这里。

这是您在给我带来问题的动词后面放置的\n 的数量。

检查此链接 https://cloud.google.com/storage/docs/access-control#Construct-the-String

String url_signature = this.signString(verb + "\n\n" 
+ "application/x-www-form-urlencoded"
+ "\n" + expiration + "\n" + "/" + BUCKET_NAME + "/" + OBJECT_NAME );

关于java - Google 云存储桶 - 上传文件 - 403 Forbidden Android SignatureDoesNotMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35601700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com