@Override
 protected OAuth2AccessToken readInternal(
     Class<? extends OAuth2AccessToken> clazz, HttpInputMessage response)
     throws IOException, HttpMessageNotReadableException {
   MediaType contentType = response.getHeaders().getContentType();
   if (contentType != null && JSON_MEDIA_TYPE.includes(contentType)) {
     try {
       return getSerializationService().deserializeJsonAccessToken(response.getBody());
     } catch (SerializationException e) {
       throw new OAuth2Exception(
           "Error getting the access token, and unable to read the details of the error in the JSON response.",
           e);
     }
   } else {
     // the spec currently says json is required, but facebook, for example, still returns
     // form-encoded.
     MultiValueMap<String, String> map = FORM_MESSAGE_CONVERTER.read(null, response);
     return getSerializationService().deserializeAccessToken(map.toSingleValueMap());
   }
 }
    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
      MediaType contentType = response.getHeaders().getContentType();
      if (contentType != null
          && (response.getStatusCode().value() == 400 || response.getStatusCode().value() == 401)) {
        if (JSON_MEDIA_TYPE.includes(contentType)) {
          try {
            throw getSerializationService().deserializeJsonError(response.getBody());
          } catch (SerializationException e) {
            throw new OAuth2Exception(
                "Error getting the access token, and unable to read the details of the error in the JSON response.",
                e);
          }
        } else if (FORM_MEDIA_TYPE.includes(contentType)) {
          MultiValueMap<String, String> map = FORM_MESSAGE_CONVERTER.read(null, response);
          throw getSerializationService().deserializeError(map.toSingleValueMap());
        }
      }

      super.handleError(response);
    }
 public void doWithRequest(ClientHttpRequest request) throws IOException {
   getAuthenticationHandler().authenticateTokenRequest(this.resource, this.form, request);
   request.getHeaders().setAccept(Arrays.asList(JSON_MEDIA_TYPE, FORM_MEDIA_TYPE));
   FORM_MESSAGE_CONVERTER.write(this.form, FORM_MEDIA_TYPE, request);
 }