0

Using Apex, trying to:

  1. Create an External Credential for Client Credentials with Client Secret Flow
  2. Add a Principal with a Client Id and Client Secret
  3. Associate it with an existing Named Credential
  4. Associate it with an existing Permission Set

Here's the code I've written so far...

// Setup External Credential
ConnectApi.ExternalCredentialInput externalCredInput = new ConnectApi.ExternalCredentialInput();
externalCredInput.developerName = 'MyExternalCredential';
externalCredInput.masterLabel = 'My External Credential';
externalCredInput.authenticationProtocol = ConnectApi.CredentialAuthenticationProtocol.OAuth;
externalCredInput.authenticationProtocolVariant = ConnectApi.CredentialAuthenticationProtocolVariant.ClientCredentialsClientSecret;

// Set Scope External Credential Parameter
ConnectApi.ExternalCredentialParameterInput scopeParam = new ConnectApi.ExternalCredentialParameterInput();
scopeParam.parameterName = 'Scope';
scopeParam.parameterType = ConnectApi.ExternalCredentialParameterType.AuthParameter;
scopeParam.parameterValue = '{!$User.Id}&{!$User.Email}&{!$Organization.Id}';

// Set Identity Provider URL Parameter
ConnectApi.ExternalCredentialParameterInput authProviderUrlParam = new ConnectApi.ExternalCredentialParameterInput();
authProviderUrlParam.parameterName = 'AuthProviderUrl';
authProviderUrlParam.parameterType = ConnectApi.ExternalCredentialParameterType.AuthProviderUrl;
authProviderUrlParam.parameterValue = 'https://path.to.my.oauth.endpoint';

// Add parameters to the External Credential
externalCredInput.parameters = new List<ConnectApi.ExternalCredentialParameterInput>();
externalCredInput.parameters.add(authProviderUrlParam);
externalCredInput.parameters.add(scopeParam);

// Populate principals to connect the external credential to permissions
ConnectApi.ExternalCredentialPrincipalInput principal = new ConnectApi.ExternalCredentialPrincipalInput();
principal.sequenceNumber = 1;
principal.principalName = 'MyPrincipal';
principal.principalType = ConnectApi.CredentialPrincipalType.NamedPrincipal;

// Add Client Id
ConnectApi.ExternalCredentialParameterInput clientId = new ConnectApi.ExternalCredentialParameterInput();
clientId.parameterName = 'ClientId'; // value not accepted as an AuthParameter
clientId.parameterType = ConnectApi.ExternalCredentialParameterType.AuthParameter;
clientId.parameterValue = 'MY_CLIENT_ID';

// Add Client Secret
ConnectApi.ExternalCredentialParameterInput clientSecret = new ConnectApi.ExternalCredentialParameterInput();
clientSecret.parameterName = 'ClientSecret'; // value not accepted as an AuthParameter
clientSecret.parameterType = ConnectApi.ExternalCredentialParameterType.AuthParameter;
clientSecret.parameterValue = 'MY_SECRET';

principal.parameters = new List<ConnectApi.ExternalCredentialParameterInput>();
principal.parameters.add(clientId);
principal.parameters.add(clientSecret);

// Add Principal to External Credential
externalCredInput.principals = new List<ConnectApi.ExternalCredentialPrincipalInput>();
externalCredInput.principals.add(principal);

// Create External Credential
ConnectApi.ExternalCredential externalCred = ConnectApi.NamedCredentials.createExternalCredential(externalCredInput);

// After creating it, set the related Named Credential
NamedCredential myNamedCredential = [SELECT Id FROM NamedCredential WHERE DeveloperName = 'MyNamedCredential' LIMIT 1];
externalCred.relatedNamedCredentials = new List<ConnectApi.NamedCredential>();
ConnectApi.NamedCredential namedCredential = new ConnectApi.NamedCredential();
namedCredential.id = myNamedCredential.Id;
externalCred.relatedNamedCredentials.add(namedCredential);

// And Permission Set
PermissionSet permissionSet = [SELECT Id FROM PermissionSet WHERE Name = 'MyConnectorUser' LIMIT 1];
if (permissionSet != null) {
  List<ConnectApi.ExternalCredentialPrincipalAccess> principalAccessList = new List<ConnectApi.ExternalCredentialPrincipalAccess>();
  ConnectApi.ExternalCredentialPrincipalAccess principalAccess = new ConnectApi.ExternalCredentialPrincipalAccess();
  principalAccess.type = ConnectApi.ExternalCredentialPrincipalAccessType.PermissionSet;
  principalAccess.id = permissionSet.Id;
  principalAccessList.add(principalAccess);
  externalCred.principals[0].principalAccess = principalAccessList;
}

// And namespace
externalCred.createdByNamespace = 'myNamespace';

// Update it
ConnectApi.NamedCredentials.patchCredential(externalCred);

System.debug('External Credential created successfully');

The problem is that ClientId and ClientSecret are not valid names. I've tried camelcase, snake case as well.

Have not seen any accepted answers where the Client Id and Client Secret are set via Apex.

Anyone able to overcome this?

1

0

You must log in to answer this question.