function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rhythm SharmaRhythm Sharma 

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 typ

Hi All,
 
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());

        }

    }

}
VinayVinay (Salesforce Developers) 
Hi Rythm,

Check similar issue below and hope it helps you.

https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000A8PxhSAF

Please mark as Best Answer if above information was helpful.

Thanks,