I have this error:
System.CalloutException: Failed to create certificate java.security.cert.CertificateException: Could not decode the client certificate. Please ensure that it is a base64 representation of a PKCS12 file: toDerInputStream rejects tag type 45
I am integrating ADP into Salesforce using rest, I am trying to get access token from ADP.
Code - public class getAccessToken {
public static void getToken() {
HttpRequest request = new HttpRequest();
HttpResponse response = new HttpResponse();
Http http = new Http();
// Set the request URL
String authEndpoint = 'https://accounts.adp.com/auth/oauth/v2/token';
request.setEndpoint(authEndpoint);
// Set the request method
request.setMethod('POST');
// Set the request headers
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
// Set the request body
String payload = 'grant_type=client_credentials' +
'&client_id=437134191f47' +
'&client_secret=49651ff0-70b6baac';
request.setBody(payload);
// Retrieve the AFHMutualSSLCert key file from static resource
StaticResource staticResourceAFHMutualSSLCert = [SELECT Body FROM StaticResource WHERE Name = 'AFHMutualSSLCert'];
String AFHMutualSSLCertContent = staticResourceAFHMutualSSLCert.Body.toString();
system.debug('AFHMutualSSLCertContent: '+AFHMutualSSLCertContent);
// Convert the AFHMutualSSLCertContent key to Base64-encoded string
Blob AFHMutualSSLCertKeyBlob = Blob.valueOf(AFHMutualSSLCertContent);
String AFHMutualSSLCertKeyBase64 = EncodingUtil.base64Encode(AFHMutualSSLCertKeyBlob);
system.debug('AFHMutualSSLCertKeyBase64: '+AFHMutualSSLCertKeyBase64);
// Retrieve the private key file from static resource
StaticResource staticResourcePrivateKey = [SELECT Body FROM StaticResource WHERE Name = 'AFHPrivateKey'];
String privateKeyContent = staticResourcePrivateKey.Body.toString();
system.debug('privateKeyContent: '+privateKeyContent);
// Convert the private key to Base64-encoded string
Blob privateKeyBlob = Blob.valueOf(privateKeyContent);
String privateKeyBase64 = EncodingUtil.base64Encode(privateKeyBlob);
system.debug('privateKeyBase64: '+privateKeyBase64);
request.setClientCertificate(AFHMutualSSLCertKeyBase64, privateKeyBase64);
system.debug('request: '+request.getBody());
// Send the request and get the response
response = http.send(request);
system.debug('response: '+response);
// Parse the response
if (response.getStatusCode() == 200) {
// Successful response
Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
String accessToken = (String) responseMap.get('access_token');
System.debug('Access Token: ' + accessToken);
// Use the access token for further API requests
} else {
// Error handling for failed request
System.debug('Error: ' + response.getBody());
}
}
}