Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON parse error: Cannot deserialize instance of java.lang.String out of START_OBJECT token #25

Open
Gyurmatag opened this issue Jan 25, 2020 · 1 comment

Comments

@Gyurmatag
Copy link

Gyurmatag commented Jan 25, 2020

Hi!

I have the fresh code base up and running with everything configured, but when I try to Log in or Sign up I get this error log:

org.springframework.http.converter.HttpMessageNotReadableException: An error occurred reading the OAuth 2.0 Error: JSON parse error: Cannot deserialize instance of java.lang.Stringout of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance ofjava.lang.Stringout of START_OBJECT token at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 1, column: 10] (through reference chain: java.util.LinkedHashMap["error"]); nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance ofjava.lang.Stringout of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance ofjava.lang.String out of START_OBJECT token at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 1, column: 10] (through reference chain: java.util.LinkedHashMap["error"])

Maybe Facebook sending another format back and an update needed.
Hint: Google login/signup working fine...
Can you please fix this problem?

Thank you very much!

@ghassen1khalil
Copy link

I finally succeeded to resolve this problem thank's to @Grauzone's pull-request
You must add these methods in your SecurityConfig class :

private OAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
        enhanceJsonMessageConverter(restTemplate);
        customOAuth2UserService.setRestOperations(restTemplate);
        return customOAuth2UserService;
    }

    private void enhanceJsonMessageConverter(RestTemplate restTemplate) {
        // NOTE:
        // Facebook's UserInfo API -> https://graph.facebook.com/me
        // returns "text/javascript; charset=UTF-8" for the "content-type" response header
        // even though the content is JSON. This is not correct and should be reported to Facebook to fix.
        //
        // This is a temporary workaround that adds "text/javascript; charset=UTF-8"
        // as a supported MediaType in MappingJackson2HttpMessageConverter,
        // which is used to convert the UserInfo response to a Map.

        HttpMessageConverter<?> jsonMessageConverter = restTemplate.getMessageConverters().stream()
                .filter(c -> c instanceof MappingJackson2HttpMessageConverter)
                .findFirst()
                .orElse(null);

        if (jsonMessageConverter == null) {
            return;
        }

        List<MediaType> supportedMediaTypes = new ArrayList<>(jsonMessageConverter.getSupportedMediaTypes());
        supportedMediaTypes.add(MediaType.valueOf("text/javascript;charset=UTF-8"));
        ((AbstractHttpMessageConverter) jsonMessageConverter).setSupportedMediaTypes(supportedMediaTypes);
    }

And then replace .userService(customOAuth2UserService) by .userService(oauth2UserService())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants