Ask Your Question
0

authentication keystone httpUrlconnection java

asked 2014-01-29 19:45:05 -0500

chaari-rihab gravatar image

Hi! I'm trying to get connected to openstack through a curl. I used the following curl:

curl -i 'http://10.0.10.15:5000/v2.0/tokens' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": { "passwordCredentials": {"username": "adm", "password": "pwd"},"tenantName":"adm"}}'

I would like to convert this curl to a java code using httpurlConnection. I used the following code:

                           String url="http://10.0.10.15:5000/v2.0/tokens";
               URL object=new URL(url);
            HttpURLConnection con = (HttpURLConnection) object.openConnection();
            String username="adm";
            String password="pwd";
            String userpass=username+":"+password;
            String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setRequestProperty("Content-Type", "application/json; charset=utf8");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestMethod("POST");
            con.setRequestProperty ("Authorization", basicAuth);
            con.setRequestProperty("X-Auth-Token", "admin");
                            /.....

I didn't write all the code. any way, my problem here is that I get 401 not authorized. while the curl gives me the following result:

HTTP/1.1 200 OK Vary: X-Auth-Token Content-Type: application/json Content-Length: 1715 Date: Wed, 29 Jan 2014 16:15:17 GMT

{"access": {"token": {"issued_at": "2014-01-29T16:15:17.453027", "expires": "2014-01-30T16:15:17Z", "id": "c1aa6b4d029341969057c5241e6ac1b7", "tenant": {"description": "Admin Tenant", "enabled": true, "id": "3d8a902da948405fb3946dd0073cba37", "name": "admin"}}, "serviceCatalog": [{"endpoints": [{"adminURL": "http://127.0.0.1:9292/v2", "region": "region", "internalURL": "http://127.0.0.1:9292/v2", "id": "2197e6da03da464799b38ce984866220", "publicURL": "http://10.0.2.15:9292/v2"}], "endpoints_links": [], "type": "image", "name": "glance"}, {"endpoints": [{"adminURL": "http://127.0.0.1:8774/v2/3d8a902da948405fb3946dd0073cba37", "region": "region", "internalURL": "http://127.0.0.1:8774/v2/3d8a902da948405fb3946dd0073cba37", "id": "a5baa32af8dd4a2995e8332ee5cbb679", "publicURL": "http://10.0.2.15:8774/v2/3d8a902da948405fb3946dd0073cba37"}], "endpoints_links": [], "type": "compute", "name": "nova"}, {"endpoints": [{"adminURL": "http://127.0.0.1:9696/", "region": "region", "internalURL": "http://127.0.0.1:9696/", "id": "7be2d3fa8a3440e5b60f32156ec472ea", "publicURL": "http://10.0.2.15:9696/"}], "endpoints_links": [], "type": "network", "name": "quantum"}, {"endpoints": [{"adminURL": "http://127.0.0.1:35357/v2.0", "region": "region", "internalURL": "http://127.0.0.1:5000/v2.0", "id": "095f2747869d4807a1fae6f6a27bbd59", "publicURL": "http://10.0.2.15:5000/v2.0"}], "endpoints_links": [], "type": "identity", "name": "keystone"}], "user": {"username": "admin", "roles_links": [], "id": "6411b776b8214a369834fbddb0eb24f3", "roles": [{"name": "_member_"}, {"name": "admin"}], "name": "admin"}, "metadata": {"is_admin": 0, "roles": ["9fe2ff9ee4384b1894a90878d3e92bab", "f7fb48761caf4dbaaede935691605250"]}}}

I don't know am I missing some thing in the code. Is there a problem in authentication code? Thank you in advance!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-02-05 21:54:09 -0500

haneef gravatar image

you are using basic auth. You need to post json data. Java is too verbose to write the whole code.

If you are just trying it as an example, then have the json in file, and read the the file contents in string. Then post the string Our u can build json using GsonBuilder

sendRequest(String requestReadFromFile){

                   String url="http://10.0.10.15:5000/v2.0/tokens";
      URL object=new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();

con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json; charset=utf8");
con.setRequestProperty("Accept", "application/json");
con.setRequestMethod("POST");

//Send request DataOutputStream wr = new DataOutputStream ( con.getOutputStream ()); wr.writeBytes (requestReadFromFile); wr.flush (); wr.close ();

  //Get Response    
  InputStream is = con.getInputStream();
  BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  String line;
  StringBuffer response = new StringBuffer(); 
  while((line = rd.readLine()) != null) {
    response.append(line);
    response.append('\r');
  }
  rd.close();

System.out.println(respone)

}

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2014-01-29 19:45:05 -0500

Seen: 1,143 times

Last updated: Feb 05 '14