3

I'm trying to use Named Credentials to authenticate to an external API. I read some tutorials, found out that they where basically all indicating the same method, so here is what I did so far :

In Named credentials, I created an external credentials with a "custom" Authentication Protocol, then created to principals, one for the permission Set, and the other one for the authentication with to parameters, Username and Password.

Also created a custom Header called "Authorization" with a formula as value :

{!'Basic ' & BASE64ENCODE(BLOB($Credential.BasicAuth.Username & ':' & $Credential.BasicAuth.Password))}

I also created the named credential, which I call from my Apex class.

Http http = new Http();
HttpRequest request = newRequest('POST');

request.setEndpoint('callout:' + NAMED_CREDENTIAL_NAME);

HttpResponse res = http.send(request);

My problem is that when I try to call the webservice I get an error saying "Field BasicAuth.Username does not exist. Check spelling."

The user is assigned with the right permission set. I've enabled the external credential Principal access in the profile...

I'm running out of ideas. Has anyone faced that problem before? What am I missing?

**** Edit ****

It seems that the problem is coming from one the Principals in my external Credential. Indeed, if I put directly the username and password in the formula, it works. But when I try to use the principal that I called Authentication, with 2 authentication paramaters (Username and Password) it doesn't work anymore. I found that odd from the beginning that after I saved this principal, when I edit it, the values of both parameters is empty. Any idea on what might cause this? enter image description here

thanks for your time and answers.

5
  • You're not in a scratch org are you? Wondering if a namespace is coming into it or something.
    – Matt Lacey
    Jul 11, 2023 at 8:39
  • I am actually... If the problem concerns the namespace, what do you think I should do?
    – CelineD
    Jul 13, 2023 at 7:13
  • Do the new named creds support basic auth? Might be worth trying a legacy one.
    – Matt Lacey
    Jul 13, 2023 at 9:08
  • i definitely have noticed that clicking EDIT wipes out the previous values for the parameters even if you click Cancel (I know, seems hard to believe). In @Alberto answer, I have generate Authorization Header unchecked and Allow Formulas in Http Headers checked and that works for me
    – cropredy
    Jul 13, 2023 at 23:52
  • @CelineD Any workaround for it? I'm also facing this issue, param values are wiped out if there are 2 or more params in a Principal. Jul 26, 2023 at 3:48

3 Answers 3

1

The format for the External Credential custom header using Basic Auth should have a Name of Authorization and the Value should be something like this:

{!'Basic ' & BASE64ENCODE(BLOB($Credential.EXTERNAL_CRED_NAME.PRINCIPAL_KEY_1 & ':' & $Credential.EXTERNAL_CRED_NAME.PRINCIPAL_KEY_2))}

Yours would be:

{!'Basic ' & BASE64ENCODE(BLOB($Credential.EXTERNAL_CRED_NAME.Username & ':' & $Credential.EXTERNAL_CRED_NAME.Password))}

... but replace EXTERNAL_CRED_NAME with the credential name, NOT the Principal name.

And then the related Named Credential only needs to have checked:

  1. Allow Formulas in HTTP Header

(you can skip Generate Authorization Header)

Then you need to ensure that each user who may run your named credential has access to the External Credential. You could do this from a Permission Set or directly on a Profile under External Credential Principal Access. It will show up in the list as EXTERNAL CRED NAME - PRINCIPAL NAME.

If you've done this part correctly, then the access members will show up in the External Credential Principal as in the screenshot attached.

enter image description here

0

It looks correct. Did you also grant read access to the object "User External Credentials"?enter image description here

Only thing i can think of after that is the API name of the External Credential is not "BasicAuth"?

I have hade some copy paste issues from teams with empty characters. So could be worth a try and re-input it.

{!'Basic ' & BASE64ENCODE(BLOB($Credential.YOUR_EXTERNAL_CRED.Username & ':' & $Credential.YOUR_EXTERNAL_CRED.Password))}
2
  • Yes I did... The External Credential is indeed BasicAuth, I'll try to write it and not copy/paste it. thanks
    – CelineD
    Jul 6, 2023 at 10:07
  • Well, it was a dead end
    – CelineD
    Jul 6, 2023 at 10:41
0

In a scratch org you can't use your own namespace, so that's not the issue. I think you can look at the Formula fields in the header. Check "Allow Formulas in HTTP Header" checkbox in the NamedCredential page:

enter image description here

then try to set the authorization header as follows (with the formula fields):

Blob header = Blob.valueOf('{!$Credential.BasicAuth.Username}' + ':' + '{!$Credential.BasicAuth.Password}'); 
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(header );

You must log in to answer this question.

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