java - Rest Api call gives error 400 using Spring Oauth2 -


i'm building rest api using spring security oauth2 secure it.

the following curl command runs succesfully , token:

curl -x post -vu clientapp:123456 http://localhost:8080/dms-application-0.0.1-snapshot/oauth/token -h "accept: application/json" -d "password=spring&username=roy&grant_type=password&scope=read%20write&client_secret=123456&client_id=clientapp" 

the following test token runs succesfully:

@test public void getaccesstoken() throws exception {     string authorization = "basic " + new string(base64utils.encode("clientapp:123456".getbytes()));     string contenttype = mediatype.application_json + ";charset=utf-8";      // @formatter:off     string content = mvc             .perform(                     post("/oauth/token")                             .header("authorization", authorization)                             .contenttype(                                     mediatype.application_form_urlencoded)                             .param("username", "roy")                             .param("password", "spring")                             .param("grant_type", "password")                             .param("scope", "read write")                             .param("client_id", "clientapp")                             .param("client_secret", "123456"))             .andexpect(status().isok())             .andexpect(content().contenttype(contenttype))             .andexpect(jsonpath("$.access_token", is(notnullvalue())))             .andexpect(jsonpath("$.token_type", is(equalto("bearer"))))             .andexpect(jsonpath("$.refresh_token", is(notnullvalue())))             .andexpect(jsonpath("$.expires_in", is(greaterthan(4000))))             .andexpect(jsonpath("$.scope", is(equalto("read write"))))             .andreturn().getresponse().getcontentasstring();      // @formatter:on      string token= content.substring(17, 53); } 

however, when calling rest end point externally webapp using spring resttemplate gives me http error 400. below code:

@requestmapping(value = "/authentication", method = requestmethod.post, consumes = mediatype.application_json_value, produces = mediatype.application_json_value) @responsebody public responseentity authenticate(@requestbody credentialsdto credentials) {     try {          string email = credentials.getemail();         string password = credentials.getpassword();         string tokenurl = "http://" + env.getproperty("server.host") + ":8080" + "/dms-application-0.0.1-snapshot" + "/oauth/token";          // create request body         jsonobject request = new jsonobject();         request.put("username", "roy");         request.put("password", "spring");         request.put("grant_type","password");         request.put("scope","read write");         request.put("client_secret","123456");         request.put("client_id","clientapp");           // set headers         httpheaders headers = new httpheaders();          string authorization = "basic " + new string(base64utils.encode("clientapp:123456".getbytes()));         string contenttype = mediatype.application_form_urlencoded.tostring();         headers.set("authorization",authorization);         headers.set("accept","application/json");         headers.set("content-type",contenttype);          httpentity<string> entity = new httpentity<string>(request.tostring(), headers);          // send request , parse result         responseentity<string> loginresponse = restclient.exchange(tokenurl, httpmethod.post, entity, string.class);        // restclient.postforentity(tokenurl,entity,string.class,)         if (loginresponse.getstatuscode() == httpstatus.ok) {             //jsonobject userjson = new jsonobject(loginresponse.getbody());             string response = loginresponse.getbody();             return responseentity.ok(response);         } else if (loginresponse.getstatuscode() == httpstatus.unauthorized) {             // nono... bad credentials             return responseentity.status(httpstatus.unauthorized).build();          }      } catch (exception e) {         e.printstacktrace();         return new responseentity(httpstatus.internal_server_error);     }     return null; } 

and error get:

"missing grant type"

any ideas of can wrong or other ways it? because i'm stuck on this.

thank you

try this:

multivaluemap<string, string> map = new linkedmultivaluemap<string, string>(); map.add("username", "roy"); map.add("password", "spring"); map.add("grant_type", "password"); map.add("scope", "read write"); map.add("client_secret","123456"); map.add("client_id","clientapp");             httpentity request = new httpentity(map, headers); 

one more thing, when ask token make sure not send json request, header:

headers.add("content-type", "application/x-www-form-urlencoded");  

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -