我想用java发送json格式的命令到keystone给指定的用户加admin角色,我找到了一个rackspace发布的keystone API extension,其中有"add global role to user" API, 网址是:
http://docs.rackspace.com/openstack-extensions/auth/OS-KSADM-admin-devguide/content/PUT_addUserRole_v2.0_users__userId__roles_OS-KSADM__roleId__Admin_API_Service_Developer_Operations-d1e1357.html
我试着按照API中的URI格式发送请求,可是得不到结果,我的代码:
其中c53612ae37ec4721a7a72863a1c48726为User_id, 3a6089fd5802419ea97d14a4909b9853为admin_id
private static void addRole(){
try {
//创建连接
URL url = new URL(key_admin_url + "/users/" + "c53612ae37ec4721a7a72863a1c48726" + "/roles/OS-KSADM/" + "3a6089fd5802419ea97d14a4909b9853");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("PUT");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/json");
connection.setRequestProperty("X-Auth-Token",
"012345SECRET99TOKEN012345");
connection.connect();
//POST 请求
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
out.writeBytes("");
out.flush();
out.close();
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
System.out.println(sb);
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
请问有人这样连接过么? 正确的发送方式应该是怎样的呢?谢谢
json
java
api
rackspace
keystone
|
|