I need to call the same org using named credential password authentication,. Can anyone explain if i need to pass a api for the password where could I get that api for same salesforce org?
2 Answers
As you are accessing the API from the same Salesforce org, you can use SessionId, which should be the recommended approach. However, if you still wish to use Connected App and named credentials please follow the below step:
- Create a Connected App with OAuth Scope of "Full Access" and "refresh token".
- Create an Auth Provider with default scope as refresh and full.
- Update the connected app with callback URL from auth provider.
- Create a Named Credential with the same scope as above and Authentication protocol as oAuth 2.0. Please tick "Start Authentication Flow on Save" as true. This will redirect you to authenticate the Salesforce login using oAuth webserver flow.
- Use the named credentials in your callout.
Salesforce as a provider does not support Password Authentication. Hence Named Credential with Password authentication on the same org will not work and it will throw an error as below: [{"message":"INVALID_HEADER_TYPE","errorCode":"INVALID_AUTH_HEADER"}]
You may use oAuth2.0 or any other authentication approach for named credentials. Alternatively, you can use the sessionid(UserInfo.getSessionId()) to authenticate the callout as you are consuming the API from the same Salesforce org.
-
response==[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}] System.HttpResponse[Status=Unauthorized, StatusCode=401] It showing this error when I tested. That means the authentication is not correct as i got.So that means I have to createa connected app and get the key and then provide it in Auth provider.Am i right?– BasuCommented Nov 7, 2021 at 17:23