我正在尝试将 Spring Boot 与 Salesforce 集成,但即使凭据正确OAuth2RestTemplate
,它也会给我错误。Access token denied
调试后,我发现 Salesforce 正在发送 400 HTTP 状态代码。
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());
}
}
}
但是,如果我遵循这种技术,那么一切正常。我使用的唯一原因OAuth2RestTemplate
是因为它会自动处理令牌刷新。如果有其他方法可以在不使用 OAuthRestTemplate 的情况下这样做,那么也请告诉我。