0

我正在尝试将 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 的情况下这样做,那么也请告诉我。

4

1 回答 1

0

最后我能够解决这个问题。我发布我的答案,希望它会帮助某人。

ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();

resource.setAccessTokenUri("https://test.salesforce.com/services/oauth2/token");
resource.setClientId("client_id");
resource.setClientSecret("client_secret");
resource.setClientAuthenticationScheme(AuthenticationScheme.form);
resource.setUsername("username");
resource.setPassword("password");

OAuth2RestTemplate operations = new OAuth2RestTemplate(resource(), new DefaultOAuth2ClientContext(new DefaultAccessTokenRequest()));
operations.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); // Not required if you are not doing any conversion
operations.getMessageConverters().add(new StringHttpMessageConverter()); // Not required if you are not doing any conversion

try {

    System.out.println(auth2RestTemplate.getAccessToken());

} catch (Throwable t) {

    System.out.println(t.getMessage());
}
于 2018-05-11T19:09:59.800 回答