我想使用 appengine 的 Json API 将 ACL 添加到 Google Storage 对象。我已经尝试了以下代码,但是我得到了没有详细信息的 400 响应。我没有使用 java-client-libraries,但是我愿意尝试。下面是我的代码:
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,“消息”:“必需” } }