Select your Language

Consume API in salesforce by using Named Credentials, External Credential, External Auth Identity Provider

 Step - 1 Create  External Auth Identity Provider

Go to setup --> Named Credentials --> click on tab "External Auth Identity Provider" --> new

Enter information as below:

Label: MY_AUTH_ID

Name: MY_AUTH_ID

Authentication Protocol: OAuth 2.0

Authentication Flow Type: Client Credentials

Client Id: ******************

Client Secret: ***************************

Token Endpoint URL: https://orgfarm-33b4115c6b-dev-ed.develop.my.salesforce.com/services/oauth2/token

Save

Step - 2 Create  External Credentials

Go to setup --> Named Credentials --> click on tab "External Credentials" --> new

Enter information as below:

Label: My_External_ID

Name: My_External_ID

Authentication Protocol: OAuth 2.0

Authentication Flow Type: Configured in an External Auth Identity Provider

External Auth Identity Provider: Lookup of the same MY_AUTH_ID which you have created earlier.

Step - 3 Create  Principal , for enabling the access .

Go to --> External Credentials "My_External_ID" which you have created above" --> Scroll down and look principal 

Enter information as below:

Parameter Name: Principal Access

Sequence Number: 1

Step - 4 Create  Named Credentials

Go to setup --> Named Credentials --> new

Enter information as below:

Label: Name_API

Name: Name_API

URL: https://orgfarm-33b4115c6b-dev-ed.develop.my.salesforce.com

Enabled for Callouts: Check

External Credential: Which created above (My_External_ID)

Save

Step - 5 Create  permission set and assign to the user and enable the "External Credential Principal Access" save.

Step - 6 Create a apex code for callout as below:

-------------------------------------------------------------------------

public class MyNamedCreds {

    public static void makeGetCallout() {

        HttpRequest req = new HttpRequest();

        req.setEndpoint('callout:Name_API/services/data/v64.0/query?q=select+name+from+account+order+BY+name+limit+4');

        req.setMethod('GET');

        Http http = new Http();

        HttpResponse res = http.send(req);

        if (res.getStatusCode() == 200) {

            System.debug('Response: ' + res.getBody());

        } else {

            System.debug('Error: ' + res.getStatus());

        }

    }

}

---------------------------------------------------------------

Calling this above callout by using the below code:

MyNamedCreds.makeGetCallout();




No comments:

Post a Comment