我想使用appengine中的Json API將ACL添加到Google Storage對象。我嘗試了下面的代碼,但是我得到了一個沒有任何細節的400響應。我沒有使用java-client-libraries,但是我願意嘗試。以下是我的代碼:在Appengine中使用JSON API爲Google存儲對象設置ACL
public static void updateACL(String bucket, String object,
List<String> emails) {
try {
ArrayList scopes = new ArrayList();
scopes.add("https://www.googleapis.com/auth/devstorage.full_control");
AppIdentityService appIdentity = AppIdentityServiceFactory
.getAppIdentityService();
AppIdentityService.GetAccessTokenResult accessToken = appIdentity
.getAccessToken(scopes);
// The token asserts the identity reported by
// appIdentity.getServiceAccountName()
logger.log(Level.WARNING, "bucket: "+bucket+" object: "+object+ " email: "+emails.get(0));
JSONObject request = new JSONObject();
request.put("entity", "user-" + emails.get(0));
request.put("roles", "READER");
URL url = new URL("https://www.googleapis.com/storage/v1beta1/b/"
+ bucket + "/o/"+object+"/acl?key=" + API_KEY);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("Authorization", "OAuth "
+ accessToken.getAccessToken());
String urlParameters = "bucket=" + bucket + "&object=" + object;
OutputStreamWriter writer = new OutputStreamWriter(
connection.getOutputStream());
request.write(writer);
writer.close();
logger.log(Level.WARNING, connection.getResponseMessage());
logger.log(Level.WARNING,
String.valueOf(connection.getResponseCode()));
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// Note: Should check the content-encoding.
// JSONTokener response_tokens = new
// JSONTokener(connection.getInputStream());
// JSONObject response = new JSONObject(response_tokens);
// return (String) response.get("id");
return;
} else {
Scanner s;
s = new Scanner(connection.getErrorStream());
s.useDelimiter("\\Z");
String response = s.next();
s.close();
throw new Exception(connection.getResponseCode()+" "+connection.getResponseMessage()+
response);
}
} catch (Exception e) {
logger.log(Level.WARNING, "exception: "+e.getMessage());
}
}
我得到的答案是400,但細節沒有任何幫助。
「錯誤」:{ 「錯誤」:[ { 「結構域」: 「全局」, 「原因」: 「需要」, 「消息」: 「必需」 } ] 「代碼」:400, 「消息」: 「必需」 }}
嘗試將角色更改爲request.put中的角色(「角色」,「讀者」) – fejta