我想知道是否有人能告诉我正确的语法&格式化服务帐户以将POST对象发送到存储桶请求?我正在使用the HttpComponents library以编程方式尝试它。我设法从我的GoogleCredential获取令牌,但每次构建POST请求时,我都会得到:
HTTP / 1.1 403 Forbidden
<?xml version='1.0' encoding='UTF-8'?><Error><Code>AccessDenied</Code><Message>Access denied.</Message><Details
&GT;斗名</Details></Error
&GT;
描述请求方法的Google documentation,提到使用html表单发布,但我希望这并不是建议完成工作的唯一方法。我知道HttpComponents有一种方法可以使用UrlEncodedFormEntity显式创建表单数据,但它不支持多部分数据。这就是我使用MultipartEntity类的原因。我的代码如下:
MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
String token = credential.getAccessToken();
entity.addPart("Authorization", new StringBody("OAuth " + token));
String date = formatDate(new Date());
entity.addPart("Date", new StringBody(date));
entity.addPart("Content-Type", new StringBody("multipart/form-data"));
entity.addPart("bucket", new StringBody(bucket));
entity.addPart("key", new StringBody("fileName"));
entity.addPart("success_action_redirect", new StringBody("/storage"));
File uploadFile = new File("pathToFile");
FileBody fileBody = new FileBody(uploadFile, "text/xml");
entity.addPart("file", fileBody);
httppost.setEntity(entity);
System.out.println("Posting URI = "+httppost.toString());
HttpResponse response = client.execute(httppost);
HttpEntity resp_entity = response.getEntity();
正如我所提到的,我能够得到一个真正的令牌,所以我很确定问题在于我是如何形成请求的,而不是没有经过适当的身份验证。
请记住:
感谢阅读,感谢您的帮助!
答案 0 :(得分:2)
看起来您正在混淆PUT
和POST
方法,因为您正在使用仅适用于PUT的OAuth访问令牌和仅适用于POST的表单字段。
上传对象的最简单方法是使用以下内容:
HttpPut put = new HttpPut("https://storage.googleapis.com/" + BUCKET + "/" + OBJECT);
put.addHeader("Authorization", "Bearer " + credential.getAccessToken());
put.setEntity(new StringEntity("object data"));
client.execute(put);
除了使用PUT
之外,还可以创建一个签名的HTML表单,用户可以使用POST
上传对象。您的表单需要signed policy document类似于以下内容:
PolicyDocument = {"expiration": "2010-06-16T11:11:11Z",
"conditions": [
["starts-with", "$key", "" ],
{"acl": "bucket-owner-read" },
{"bucket": "travel-maps"},
{"success_action_redirect": "http://www.example.com/success_notification.html" },
["eq", "$Content-Type", "image/jpeg" ],
["content-length-range", 0, 1000000]
]
}
Policy = Base_64_Encoding_Of(PolicyDocument)
MessageDigest = Sha256_With_RSA(SecretKey, Policy)
Signature = Base64_Encoding_Of(MessageDigest)
导致以下HTML:
<form action="http://travel-maps.storage.googleapis.com" method="post" enctype="multipart/form-data">
<input type="text" name="key" value="">
<input type="hidden" name="bucket" value="travel-maps">
<input type="hidden" name="Content-Type" value="image/jpeg">
<input type="hidden" name="GoogleAccessId" value="1234567890123@developer.gserviceaccount.com">
<input type="hidden" name="acl" value="bucket-owner-read">
<input type="hidden" name="success_action_redirect" value="http://www.example.com/success_notification.html">
<input type="hidden" name="policy" value="ajUJTm9jAHADNmF0aW9uIjogIjIwMTAtMDYtMTZUMTSAMPLEE6MTE6MTFaIiwNCSAMPLEiAgWyJzdGFydSAMPLEAiaHR0cDovL3maWNhSAMPLEIiB9LASAMPLEWN0aW9uX3JlZGlyZW">
<input type="hidden" name="signature" value="BSAMPLEaASAMPLE6SAMPLE+SAMPPLEqSAMPLEPSAMPLE+SAMPLEgSAMPLEzCPlgWREeF7oPGowkeKk7J4WApzkzxERdOQmAdrvshKSzUHg8Jqp1lw9tbiJfE2ExdOOIoJVmGLoDeAGnfzCd4fTsWcLbal9sFpqXsQI8IQi1493mw=">
<input name="file" type="file">
<input type="submit" value="Upload">
</form>
请注意策略和签名字段以及与策略文档中指定的条件匹配的其他隐藏字段。具体为bucket == travel-maps
,acl == bucket-owner-read
等