I am trying to integrate Spring Boot with Salesforce using OAuth2RestTemplate
but it is giving me Access token denied
error even though credentials are correct. Upon debugging I found that Salesforce is sending 400 HTTP status code.
package com.sentryds.advis.salesforce;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails;
import org.springframework.security.oauth2.common.AuthenticationScheme;
@SpringBootApplication
public class SalesforceApplication {
public static void main(String[] args) {
SpringApplication.run(SalesforceApplication.class, args);
ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();
resourceDetails.setAuthenticationScheme(AuthenticationScheme.form);
resourceDetails.setAccessTokenUri("https://test.salesforce.com/services/oauth2/token");
resourceDetails.setGrantType("password");
resourceDetails.setClientId("xxxxxxxx");
resourceDetails.setClientSecret("xxxxxxxx");
resourceDetails.setUsername("xxxxxxxx");
resourceDetails.setPassword("xxxxxxxx");
OAuth2RestTemplate auth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
try {
System.out.println(auth2RestTemplate.getAccessToken());
} catch (Throwable t) {
System.out.println(t.getMessage());
}
}
}
However if I following this technique then everything works. Only reason I am using OAuth2RestTemplate
is because it takes care of the token refresh automatically. If there is another way to do so without using OAuthRestTemplate then please let me know that as well.