Manage an OAuth Named Credential

Here are some examples of things that you can do with named and external credentials.

  • Refresh a token
  • Delete user external credentials <!-- These two examples were removed for 244 (Spring '23), but should be added back in for 246 (Winter '24). -->
<!-- - Change the label of an external credential - Change the callout options of a named credential -->

Example: Refresh a Token

Here’s how to programmatically refresh a token.

String externalCredential = 'GitHubOAuth';
String principalName = 'Markdown Access';
ConnectApi.CredentialPrincipalType principalType = ConnectApi.CredentialPrincipalType.NamedPrincipal;

ConnectApi.CredentialInput credentialInput = new ConnectApi.CredentialInput();
credentialInput.externalCredential = externalCredential;
credentialInput.principalName = principalName;
credentialInput.principalType = principalType;
credentialInput.authenticationProtocol = ConnectApi.CredentialAuthenticationProtocol.OAuth;
credentialInput.credentials = new Map<String, ConnectApi.CredentialValueInput>();

ConnectApi.Credential refreshedCredential = ConnectApi.NamedCredentials.createCredential(credentialInput, ConnectApi.CreateCredentialAction.Refresh);

Example: Delete User External Credentials

This method deletes the user external credentials, which store the encrypted access tokens used for named credential callouts. It doesn't delete the external credential itself. Apex can be used only to create a named credential, not edit or delete one.

ConnectApi.NamedCredentials.deleteCredential('GitHubOAuth', 'Markdown Access', principalType);

See Also

<!-- These two examples were removed for 244 (Spring '23), but should be added back in for 246 (Winter '24) --> <!-- ## Example: Change the Label of an External Credential ```apex ConnectApi.ExternalCredentialInput externalCredentialInput = new ConnectApi.ExternalCredentialInput(); externalCredentialInput.developerName = 'GitHubOAuth'; externalCredentialInput.masterLabel = 'GitHub OAuth New Label'; externalCredentialInput.authenticationProtocol = ConnectApi.CredentialAuthenticationProtocol.OAuth; externalCredentialInput.principals = new List<ConnectApi.ExternalCredentialPrincipalInput>(); externalCredentialInput.parameters = new List<ConnectApi.ExternalCredentialParameterInput>(); ConnectApi.ExternalCredentialParameterInput authProviderParam = new ConnectApi.ExternalCredentialParameterInput(); authProviderParam.parameterName = 'GitHubProvider'; authProviderParam.parameterType = ConnectApi.ExternalCredentialParameterType.AuthProvider; authProviderParam.parameterValue = 'GitHubProvider'; externalCredentialInput.parameters.add(authProviderParam); ConnectApi.ExternalCredentialPrincipalInput principalOne = new ConnectApi.ExternalCredentialPrincipalInput(); principalOne.principalName = 'Developer Access'; principalOne.principalType = ConnectApi.CredentialPrincipalType.PerUserPrincipal; principalOne.sequenceNumber = 1; principalOne.parameters = new List<ConnectApi.ExternalCredentialParameterInput>(); ConnectApi.ExternalCredentialParameterInput scopeParam = new ConnectApi.ExternalCredentialParameterInput(); scopeParam.parameterName = 'Scope'; scopeParam.parameterType = ConnectApi.ExternalCredentialParameterType.AuthParameter; scopeParam.parameterValue = 'gist'; principalOne.parameters.add(scopeParam); ConnectApi.ExternalCredentialPrincipalInput principalTwo = new ConnectApi.ExternalCredentialPrincipalInput(); principalTwo.principalName = 'Markdown Access'; principalTwo.principalType = ConnectApi.CredentialPrincipalType.NamedPrincipal; principalTwo.sequenceNumber = 2; externalCredentialInput.principals.add(principalOne); externalCredentialInput.principals.add(principalTwo); ConnectApi.ExternalCredential updatedExternalCredential = ConnectApi.NamedCredentials.updateExternalCredential('GitHubOAuth', externalCredentialInput); ``` ## Example: Change Callout Options for a Named Credential ```apex ConnectApi.NamedCredentialInput namedCredentialInput = new ConnectApi.NamedCredentialInput(); namedCredentialInput.developerName = 'githubAPI'; namedCredentialInput.masterLabel = 'GitHub API Update Label'; namedCredentialInput.type = ConnectApi.NamedCredentialType.SecuredEndpoint; namedCredentialInput.calloutUrl = 'https://api.github.com'; namedCredentialInput.externalCredentials = new List<ConnectApi.ExternalCredentialInput>(); ConnectApi.ExternalCredentialInput externalCredential = new ConnectApi.ExternalCredentialInput(); externalCredential.developerName = 'GitHubOAuth'; namedCredentialInput.externalCredentials.add(externalCredential); ConnectApi.NamedCredentialCalloutOptionsInput calloutOptions = new ConnectApi.NamedCredentialCalloutOptionsInput(); calloutOptions.allowMergeFieldsInBody = true; calloutOptions.allowMergeFieldsInHeader = true; calloutOptions.generateAuthorizationHeader = true; namedCredentialInput.calloutOptions = calloutOptions; ConnectApi.NamedCredential createdNamedCredential = ConnectApi.NamedCredentials.updateNamedCredential('githubAPI', namedCredentialInput); ``` -->