使用Android将新用户添加到KeyStone-OpenStack

时间:2013-11-22 02:10:20

标签: java android openstack openstack-swift keystone

我正在尝试使用此链接中给出的api为OpenStack创建新用户 http://docs.openstack.org/api/openstack-identity-service/2.0/content/POST_addUser_v2.0_users_Admin_API_Service_Developer_Operations-d1e1356.html

登录后,我已经传递了服务器回复的令牌。 这是我创建新用户的代码:

    Log.i("TAG","Adding new User"); 
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.1.122:35357/v2.0/users");

        try {   
             JSONObject newuser = new JSONObject();
             JSONObject userInfo = new JSONObject();        
             newuser.put("user", userInfo);

             //end of JsonArray                          

             userInfo.put("username", "Lou Mary");
             userInfo.put("email", "lagojo@owtel.com");
             userInfo.put("enabled", true);
             userInfo.put("OS-KSADM:password", "secret101");

             Log.i("TAG", "passing your data"+newuser.toString());
            // erequest.setText(auth.toString());

             StringEntity params1 = new StringEntity(newuser.toJSONString());
             params1.setContentEncoding("UTF-8");
             //params1.setContentType("application/json");

                Log.i("TAG","params" +params1); 
             httppost.setHeader("Content-type", "application/json");
             httppost.setHeader("X-Auth-Token", "MIICbgYJKoZIhvcNAQcCoIICXzCCAlsCAQExCTAHBgUrDgMCGjCCAUcGCSqGSIb3DQEHAaCCATgEggE0eyJhY2Nlc3MiOiB7InRva2VuIjogeyJpc3N1ZWRfYXQiOiAiMjAxMy0xMS0yMlQwMDo1NjoxMy42NDI2NjkiLCAiZXhwaXJlcyI6ICIyMDEzLTExLTIzVDAwOjU2OjEzWiIsICJpZCI6ICJwbGFjZWhvbGRlciJ9LCAic2VydmljZUNhdGFsb2ciOiBbXSwgInVzZXIiOiB7InVzZXJuYW1lIjogImFkbWluIiwgInJvbGVzX2xpbmtzIjogW10sICJpZCI6ICJjY2MwMjJkZGNhMzU0N2NiYmIxMmZmNTViNTZkOGI2OCIsICJyb2xlcyI6IFtdLCAibmFtZSI6ICJhZG1pbiJ9LCAibWV0YWRhdGEiOiB7ImlzX2FkbWluIjogMCwgInJvbGVzIjogW119fX0xgf8wgfwCAQEwXDBXMQswCQYDVQQGEwJVUzEOMAwGA1UECBMFVW5zZXQxDjAMBgNVBAcTBVVuc2V0MQ4wDAYDVQQKEwVVbnNldDEYMBYGA1UEAxMPd3d3LmV4YW1wbGUuY29tAgEBMAcGBSsOAwIaMA0GCSqGSIb3DQEBAQUABIGAqoSsewZ+ceYq1JXnu9OVvTJj+Aljm+rUio1biXow72iZ+MVBJKbKvlT4-2DFPC1PrCOErpX2jJ7HuiASSaBgAcROT+LmV3KNnHa+p9DCtgSBGRN7qHJpnQBXgs3tz4ZMVi3AB9i1mOmVHxeVKVfiQWt1zyis7OZPG-PZRq1DohQ=");
             httppost.setEntity((params1));

            // Execute HTTP Post Request
             Log.i("TAG", "pushing your data"); 
            HttpResponse response = httpclient.execute(httppost);
            Log.i("TAG", "Sucessful  " + response.getParams());


            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            String jsonresponse = reader.readLine();
            JSONTokener tokener = new JSONTokener(jsonresponse);
            try {
                JSONArray finalResult = new JSONArray(tokener);

                  Log.i("TAG", "Sucessfully communicated on server");

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }catch (IOException e) {        

            Log.e("", "IOException " + e.toString());
            Log.i("", "The server refused again! ");
            // TODO Auto-generated catch block
        }   
    }

但我收到此错误

  

org.json.JSONException:Value {       “错误”:{           “message”:“您提出的请求需要身份验证。”,           “title”:“未授权”,           “代码”:401       }}

我期待我传递的令牌会授权我添加新用户。 这就是我不理解错误的原因。 任何人,请帮忙。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

除了检查您是否具有目标项目的管理员角色(例如,当您请求身份验证令牌时,您需要指定租户ID),还要确保身份验证令牌未过期。

我相信你应该使用

"name", "Lou Mary"

而不是

"username", "Lou Mary"