0

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

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

    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{ "error": { "message": "您发出的请求需要身份验证。", "title": "Not Authorized", "code": 401 } }

我期望我通过的令牌将授权我添加新用户。这就是为什么我不明白这个错误。任何人,请帮助。知道如何解决这个问题吗?

4

1 回答 1

0

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

我也相信你应该使用

"name", "Lou Mary"

代替

"username", "Lou Mary"
于 2014-02-04T06:00:38.410 回答