2

I am trying to have a named credential (with external credential) and make a callout from my unmanaged package's Apex class.

Unfortunately I am getting the following error in the Apex

We couldn't access the credential(s). You might not have the required permissions, or the external credential <ExternalCredentialName> might not exist.

I create the NamedCredential and ExternalCredential using APIs

ExternalCredential

POST /services/data/v${sfApiVersion}/tooling/sobjects/ExternalCredential
{
  "FullName": "myExternalCredential",
  "Metadata": {
    "label": "myExternalCredential",
    "authenticationProtocol": "Custom",
    "externalCredentialParameters": [
      {
        "parameterType": "AuthHeader",
        "parameterName": "Username",
        "parameterValue": "myUsername",
        "sequenceNumber": 1
      },
      {
        "parameterType": "AuthHeader",
        "parameterName": "Password",
        "parameterValue": "myPassword",
        "sequenceNumber": 2
      }
    ]
  }
}

Named Credentials

POST /services/data/v56.0/tooling/sobjects/NamedCredential
{
  "FullName": "myNamedCrdential",
  "Metadata": {
    "label": "myNamedCrdential",
    "namedCredentialType": "SecuredEndpoint",
    "allowMergeFieldsInBody": false,
    "allowMergeFieldsInHeader": true,
    "generateAuthorizationHeader": true,
    "namedCredentialParameters": [
      {
        "parameterType": "Url",
        "parameterName": "Url",
        "parameterValue": "http://my.namedcredential.com/path"
      },
      {
        "parameterType": "Authentication",
        "parameterName": "Authentication",
        "externalCredential": "myExternalCredential"
      },
      {
        "parameterType": "HttpHeader",
        "parameterName": "Authorization",
        "parameterValue": "{!'Basic ' & BASE64ENCODE(BLOB($Credential.myExternalCredential.Username & ':' & $Credential.myExternalCredential.Password))}",
        "sequenceNumber": 1
      }
    ]
  }
}

And the apex code which throws the error

HTTP h = new HTTP();
HTTPRequest req = new HTTPRequest();

req.setEndpoint('callout:myNamedCrdential');
req.setMethod('POST');
req.setBody('{}');
req.setHeader('Content-Type', 'application/json');

HTTPResponse res = h.send(req);

I am the System Admin in my org, and the Credentials were created using my "Access token" (using a connected app)

Why does my ApexClass not have access to the external credential? I tried adding a permissionSet to my external credential (similar to how it is implemented here), but I am getting the same error.

<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
    <classAccesses>
        <apexClass>ClassWhichDoesTheCallout</apexClass>
        <enabled>true</enabled>
    </classAccesses>
    <hasActivationRequired>false</hasActivationRequired>
    <label>myNamedCrdential</label>
    <license>Salesforce</license>
</PermissionSet>

What am I missing here?

4
  • Does this post answer your query ? Named Credentials mapping works only with permission sets assigned directly to the user. Dec 23, 2022 at 6:23
  • @Swetha I did see that post. I tried adding a permisisonSet (not a permissionSetGroup) and it still did not work. have updated the question with details of the permissionSet)
    – SatheeshJM
    Dec 23, 2022 at 6:44
  • @SatheeshJM I have same issue as yours. Did you solve it? Jan 5 at 7:39
  • sorry unfortunately no. We ended up not using named credentials, and went ahead with protected custom setting + Basic Auth
    – SatheeshJM
    Jan 5 at 17:33

1 Answer 1

-2

Check if the profile is having access to User External Credential object access (Set Up -> Profiles -> Impacted Profile -> Object Settings -> User External Credential -> Provide Read/View All access)

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .