Rest API access token

Hi Cuba Team,

I am working on REST APIs with CUBA.

I have a CUBA application through which I can create rest-api “access_token” through a Rest Client as per documentation CUBA REST API

I am also able to create entities using the access token and the required endpoints through a Rest Client.

Now, I have created another Cuba project and I want to connect to my rest-api application Rest Template. I have created the application and I am able to connect the two, and have created fetched JSON responses using GET request.

Now I am trying to create the rest-api oAuth token using the Rest Template. The endpoint is as below:
http://localhost:8585/app/rest/v2/oauth/token

As per documentation, the body of the request should contain following parameters, and request should be application/x-www-form-urlencoded.

  • grant_type - password .
  • username - application user login.
  • password - application user password.

How do I send the request with these parameters in the body with content type application/x-www-form-urlencoded?

Below are screenshots where I have tried to accomplish this but facing Exceptions.

Exception Caught

17:59:14.092 ERROR c.h.cuba.core.sys.ServiceInterceptor - Exception:
org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : [{“error”:“invalid_request”,“error_description”:“Missing grant type”}]
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:184) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:583) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at com.company.restapicallsdemo1.service.AccessTokenServiceBean.getAccessToken(AccessTokenServiceBean.java:40) ~[app-core-0.1-SNAPSHOT.jar:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at com.haulmont.cuba.core.sys.ServiceInterceptor.aroundInvoke(ServiceInterceptor.java:90) ~[cuba-core-7.2.10.jar:7.2.10]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]

Hi,

You expect that accessToken.toString() creates correct request body, but it’s not clear what is “AccessToken” class is in your code.

Anyway, try to apply solutions from the internet, suggesting to use MultiValueMap: