2012-11-30 72 views
2

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

+0

嘗試將角色更改爲request.put中的角色(「角色」,「讀者」) – fejta

回答

4

我懷疑有一個微小的錯誤或兩個在你的代碼。在這個特定的實例中,當JSON API期望角色時,它看起來像你正在發送角色。

我發現調試有用的一件事是比較我的代碼通過網線發送的Google APIs explorer生成的HTTP請求。

  1. 導航到https://developers.google.com/storage/docs/json_api/v1/objectAccessControls/insert
  2. 接通Authorize requests using OAuth 2.0按鈕。
  3. 設置bucket,object,entityrole字段。
  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": "[email protected]", 
"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/[email protected]", 
"selfLink": "https://www.googleapis.com/storage/v1beta1/b/bucket/o/obj/acl/[email protected]", 
"bucket": "bucket", 
"object": "obj", 
"entity": "[email protected]", 
"role": "READER", 
"email": "[email protected]" 
} 

現在確保您發送過線的要求看起來是一樣的,因爲這一個。

+0

正確。就是這樣。謝謝 – Patrick

相關問題