如何用java发送JSON格式的请求到Keystone 给用户加admin角色?
我想用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 --------------------编程问答-------------------- 第一,要确定你请求的url地址是否拼接正确 /v2.0/tenants/{$tenant_id}/users/{$user_id}/roles/OS-KSADM/{$role_id}
第二,要确定请求的服务器地址及服务端口号是否写对了http://{$keystone_server}:35357
第三,要确定X-Auth-Token是使用admin账号获得的token
第四,为user添加role要用‘PUT’方法而不是你默认的‘POST’方法,这个你要好好看看API文档,学习下RESTful接口。
补充:云计算 , OpenStack