0

Getting Suggest named credentials for authentication error in my PMD script.

http http = new http();
        httpRequest request = new httpRequest();
        request.setMethod('POST');
        request.setEndpoint(endpoint);
        request.setHeader('Content-Type', 'application/json');
        request.setHeader('Authorization', accessTokenValue);
        request.setBody(jsonReplacedString);
        httpResponse res = http.send(request);

getting error in this line request.setHeader('Authorization', accessTokenValue);

Note: I have another Method which will do an HTTP call to get the access token, It's the dynamic value not hardcoded/stored anywhere.

1 Answer 1

1

This PMD error suggesting you to use NamedCredential instead of setting the auth header in the code (because NamedCredential is more secure). But, if you have a valid use case of not using NamedCredential, you could use the PMD suppress warnings annotation on the class/ method (or may be custom ruleset).

6
  • Authorization is the dynamic value, Is there any way we can achieve this? Aug 24, 2020 at 5:27
  • If you need to continue with the existing code and suppress this error, refer to pmd.github.io/latest/pmd_userdocs_making_rulesets.html & pmd.github.io/latest/pmd_userdocs_suppressing_warnings.html. But, if you want to use named credential, then could you provide some additional details?
    – arut
    Aug 24, 2020 at 5:36
  • Created named Credentials ` request.setEndpoint('callout:PayPal_Create_Payment');` and setting a Authorization Value in Header request.setHeader('Authorization', accessTokenValue); accessTokenValue --> Access token is the dynamic value which we generated each time with unique value Ex: Bearer A21AAH5GQmTn-v0CMgXuN6vTW6wv-q3iVWYHMb3vkoZ9ZUuFfwPygzGh88g4ZlBLPHxV7A_YczkXuD2-uAlCSeH1noSjZx9wA Aug 24, 2020 at 5:53
  • You don't need to pass the token via auth header explicitly in your code. Instead, enable Generate Authorization Header (under callout options) in the Named Credential. That should take care of the auth.
    – arut
    Aug 24, 2020 at 9:16
  • You Mean, we need to create Auth. Provider with key and` Secret` and in Named Credentials we need to provide Authentication Provider as Auth Provider Name right? Aug 24, 2020 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.