2

我想使用 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,“消息”:“必需” } }

4

1 回答 1

4

我怀疑您的代码中有一两个小错误。在这个特定的实例中,当 JSON api 需要角色时,您似乎正在发送角色。

我发现对调试有帮助的一件事是将我的代码通过网络发送的内容与Google API 资源管理器生成的 HTTP 请求进行比较。

  1. 导航到https://developers.google.com/storage/docs/json_api/v1/objectAccessControls/insert
  2. 打开Authorize requests using OAuth 2.0按钮。
  3. 设置bucketobjectentity字段role
  4. 点击执行。

您应该会看到生成的 HTTP 请求和响应。

要求:

POST https://www.googleapis.com/storage/v1beta1/b/bucket/o/obj/acl?key={YOUR_API_KEY}

Content-Type:  application/json
Authorization:  Bearer ya29.1111111111111111111111111111111111-aaaaaaaaaaaaa
X-JavaScript-User-Agent:  Google APIs Explorer

{
 "entity": "user-person@example.com",
 "role": "READER"
}

回复:

200 OK

- Hide headers -

cache-control:  no-cache, no-store, max-age=0, must-revalidate
content-type:  application/json; charset=UTF-8
date:  Fri, 30 Nov 2012 02:16:57 GMT
etag:  "fP_WVz7o95h5w16zKezUFJzMmHg/6CyL8wOk_60IJhaxNewPk1fHpQo"
expires:  Fri, 01 Jan 1990 00:00:00 GMT
server:  GSE

{

 "kind": "storage#objectAccessControl",
 "id": "bucket/obj/user-person@example.com",
 "selfLink": "https://www.googleapis.com/storage/v1beta1/b/bucket/o/obj/acl/user-person@example.com",
 "bucket": "bucket",
 "object": "obj",
 "entity": "user-person@example.com",
 "role": "READER",
 "email": "person@example.com"
}

现在确保您通过网络发送的请求与此请求相同。

于 2012-11-30T02:25:22.117 回答