I need to use an API that is protected by OAuth2. For this I use OAuth2RestTemplate. But I get below the error:
java.net.ConnectException: Connection timed out: connect
This is due to a proxy problem. I know how to set proxies in RestTemplate:
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("Proxy host", 8080));
clientHttpRequestFactory.setProxy (proxy); RestTemplate restTemplate = new RestTemplate (clientHttpRequestFactory);
Similarly, I tried setting for OAuth2RestTemplate :
@Bean public OAuth2RestOperations restTemplate(OAuth2ClientContext oauth2ClientContext) { OAuth2RestTemplate client = new OAuth2RestTemplate(resource(), oauth2ClientContext); SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT)); clientHttpRequestFactory.setProxy(proxy); client.setRequestFactory(clientHttpRequestFactory); return client; }
But it does not work and throws a Connection timed out exception. This is due to this first line OAuth2RestTemplate client = new OAuth2RestTemplate(resource(), oauth2ClientContext); , which is trying to get an access token, which means that proxy settings are also required. if i add below line then it works:
System.setProperty("https.proxyHost", "urproxy.com"); System.setProperty("https.proxyPort", "8080");
But I can not use the System.setProperties ("," ") parameter, because we do not have permission to install on the tomcat server.
I researched but could not find a way to set the proxy in OAuth2RestTemplate when creating this object.
Any help would be greatly appreciated. Thanks