I was trying to get the getAccessToken with spring rest template as given below. I am getting the below error, while calling oAuthRestTemplate.getAccessToken()
Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Current token (VALUE_STRING) not numeric, can not use numeric value accessors
Code:
ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
resource.setAccessTokenUri("https://../oauth2/token");
resource.setUsername("u");
resource.setPassword("p");
resource.setClientSecret("...s");
resource.setClientId("...i");
resource.setGrantType("password");
List<String> scopes = new ArrayList<String>(2);
scopes.add("write");
scopes.add("read");
resource.setScope(scopes);
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new MappingJackson2HttpMessageConverter());
messageConverters.add(new FormHttpMessageConverter());
OAuth2RestTemplate oAuthRestTemplate = new OAuth2RestTemplate(resource,clientContext);
oAuthRestTemplate.setMessageConverters(messageConverters);
try {
SSLUtil.turnOffSslChecking();
System.out.println(oAuthRestTemplate.getAccessToken());
String result = oAuthRestTemplate.getForObject("url" + "/api/version", String.class);
} catch (RestClientException e) {
e.printStackTrace();
}
Full Errortrace:
Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Current token (VALUE_STRING) not numeric, can not use numeric value accessors
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@44908881; line: 1, column: 159]; nested exception is com.fasterxml.jackson.core.JsonParseException: Current token (VALUE_STRING) not numeric, can not use numeric value accessors
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@44908881; line: 1, column: 159]
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:171)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:163)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:94)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:454)
at org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport.retrieveToken(OAuth2AccessTokenSupport.java:113)
at org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider.obtainAccessToken(ResourceOwnerPasswordAccessTokenProvider.java:47)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:142)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:118)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:216)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:168)
at com.ClientTemplate.main(ClientTemplate.java:69)
Caused by: com.fasterxml.jackson.core.JsonParseException: Current token (VALUE_STRING) not numeric, can not use numeric value accessors
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@44908881; line: 1, column: 159]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508)
at com.fasterxml.jackson.core.base.ParserBase._parseNumericValue(ParserBase.java:748)
at com.fasterxml.jackson.core.base.ParserBase.getLongValue(ParserBase.java:619)
at org.springframework.security.oauth2.common.OAuth2AccessTokenJackson2Deserializer.deserialize(OAuth2AccessTokenJackson2Deserializer.java:75)
at org.springframework.security.oauth2.common.OAuth2AccessTokenJackson2Deserializer.deserialize(OAuth2AccessTokenJackson2Deserializer.java:44)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2206)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:168)
... 11 more
Tried without messageConverters.Still the same error.
By the look of your stacktrace I can suggest that you are using spring oauth version 1.0.1 which has this code inside OAuth2AccessTokenJackson2Deserializer at line 75:
...
else if (OAuth2AccessToken.EXPIRES_IN.equals(name)) {
expiresIn = jp.getLongValue();
}
...
it expects field expires_in to be numeric. And your exception suggests that in your response it is in fact a string.
To fix this you can upgrade to latest version (2.1.1 at the moment) where they fixed this part to accept string value also:
....
else if (OAuth2AccessToken.EXPIRES_IN.equals(name)) {
try {
expiresIn = jp.getLongValue();
} catch (JsonParseException e) {
expiresIn = Long.valueOf(jp.getText());
}
}
....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With