Skip to content

Instantly share code, notes, and snippets.

@A-pZ
Last active August 7, 2023 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A-pZ/b6bee6437f8c5e00be8bcf7aac0573dc to your computer and use it in GitHub Desktop.
Save A-pZ/b6bee6437f8c5e00be8bcf7aac0573dc to your computer and use it in GitHub Desktop.
Salesforceの認証を行う
package com.github.apz.salesforcesample.model;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor @Getter @Setter @ToString
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) // 認証結果のレスポンスはSnakeCase
public class AuthenticationResult {
int statusCode;
String accessToken;
String signature;
String instanceUrl;
String id;
String tokenType;
String issuedAt;
public String bearerToken() {
return "Bearer " + accessToken;
}
}
package com.github.apz.salesforcesample.repository;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.apz.salesforcesample.config.SalesforceProperties;
import com.github.apz.salesforcesample.model.AuthenticationResult;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
@Component
@AllArgsConstructor
@Slf4j
public class SalesforceAuthentication {
SalesforceProperties salesforceProperties;
WebClient salesforceAuthWebClient;
public AuthenticationResult authentication() {
AuthenticationResult authenticationResult = salesforceAuthWebClient.post()
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(BodyInserters.fromFormData(authenticationCode()))
.retrieve()
.bodyToMono(AuthenticationResult.class)
.block();
log.info("result: {}", authenticationResult.toString());
return authenticationResult;
}
MultiValueMap<String, String> authenticationCode() {
return new LinkedMultiValueMap<>() {{
add("grant_type", salesforceProperties.getGrantType());
add("client_id", salesforceProperties.getConsumerKey());
add("client_secret", salesforceProperties.getConsumerSecret());
add("username", salesforceProperties.getMailAddress());
add("password", salesforceProperties.getPassword() + salesforceProperties.getSecurityToken());
}};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment