I want to use java to send a JSON format request to add an Admin role for openstack users, I see there is an keystone API extension that provide the "add global role to user"
API:
This is the link : 2.1.1.5. Add Global roles to a user.
but I do not know how to send this in a correct way by JSON format, the following is my code: I can get "key_admin_url"
in another method,it is like: 130.237.215.18:35357/v2.0
and there is http before 130.
user_id and role_id are two strings.
//create connection
public static void addRole(){
try{
URL url = new URL(key_admin_url + "/users/" + "user_id" + "/roles/OS-KSADM/" + "role_id");
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();
//put request
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
out.flush();
out.close();
//read response
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();
// disconnect
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();
}
}
But I did not succeed and get :java.io.IOException: Server returned HTTP response code: 501 for URL:
Is there anyone know how to send a correct JSON request to add role to users? Thank you very much.
The problem has been solved, because in URI, there is no tenant_id. So the correct URI is
URL url = new URL(key_admin_url + "/tenants/" + "tenant_id" + "/users/" + "user_id" + "/roles/OS-KSADM/" + "role_id");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.