Google Cloud Bucket - 上传文件 - 403 Forbidden Android SignatureDoesNotMatch

时间:2016-02-24 12:04:17

标签: java android file-upload google-cloud-storage google-cloud-platform

请遵循此处的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");
}

1 个答案:

答案 0 :(得分:3)

终于解决了。 没有直接的文件把我带到这里。

你在动词之后输入的数量是否为我造成了问题。

点击此链接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  );