0
votes

I'm trying to create new user to OpenStack using the api given in this link http://docs.openstack.org/api/openstack-identity-service/2.0/content/POST_addUser_v2.0_users_Admin_API_Service_Developer_Operations-d1e1356.html

I have passed the token replied by the server when I logged-in. This is my code for creating new user:

    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
        }   
    }

But I'm getting this Error

org.json.JSONException: Value{ "error": { "message": "The request you have made requires authentication.", "title": "Not Authorized", "code": 401 } }

I'm expecting that the token I've passed will authorize me from adding new user. That's why I don't understand the error. Anyone, please help. Any idea how to solve this?

1
Make sure the user executing this script has Admin role inside keystone for this project.koolhead17

1 Answers

0
votes

Apart from checking that you have the Admin role for the target project (e.g. when you request the auth token, you need to specify the tenant ID), also make sure that the auth token is not expired.

Also I believe you should use

"name", "Lou Mary"

instead of

"username", "Lou Mary"