Search Name Creadential New and Click on New Legecy
Configure :
Label and Name then
URL : Instace URL or Target Org
Like : https://your_instance_my.saleforce.com
Authentical Option :
Identity Type : Name Principal
Authentication Protocol : OAuth 2.0
Authentication Provider : Select the Auth Provider you just created in Step 2
Scope : refresh_token full
Authentication Status : you can see the Authentication Status Pending
thet meen you are not autherize
@RestResource (urlMapping = '/Calculator/*')
global class CalculatorAPI {
@HttpPost
global static ResponseWrapper calculation(){
RestRequest req = RestContext.request;
String jsonString = req.requestBody.toString();
WrapperClass wrapperObj = (WrapperClass) JSON.deserialize(jsonString, WrapperClass.class);
System.debug(' ===> Wrapper Object : '+wrapperObj);
Integer n1 = wrapperObj.num1;
Integer n2 = wrapperObj.num2;
Integer result = n1+n2;
// Return Response
ResponseWrapper resObj = new ResponseWrapper();
resObj.statusCode = '200';
resObj.status = 'success';
resObj.result = result;
resObj.successMessage = 'Successfully work message';
resObj.errorMessage = 'No error found';
return resObj;
}
public class WrapperClass{
Integer num1;
Integer num2;
}
global class ResponseWrapper{
global String statusCode;
global String status;
global Integer result;
global String successMessage;
global String errorMessage;
}
}
public class WebServiceCallout {
// Method to perform the callout
public static String getAccessToken() {
String clientId = '3MVG9VMBZCsTL9hnE2V8HY66_WhicxDiuBE1Oefmyu8FolIxGhdi6OoHgByVgksuGrwCFJXNYCroGXwVnzL2i';
String clientSecret = 'F17DC76D30DEFFB93DF4DAE935631CCE00B8BC31687B6729895F4ACD7957DA2C';
String username = 'dipak@apexcollege.com';
String password = 'ajmer@123CnmBJWWabJD8xEYyV8nEiAZZ'; // Append security token if required
String tokenUrl = 'https://login.salesforce.com/services/oauth2/token';
// HTTP callout
Http http = new Http();
HttpRequest request = new HttpRequest();
HttpResponse response;
try {
// Set the endpoint and method
request.setEndpoint(tokenUrl);
request.setMethod('POST');
// Set headers
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
// Set the body with required parameters
/*
String requestBody = 'grant_type=password' +
'&client_id=' + EncodingUtil.urlEncode(clientId, 'UTF-8') +
'&client_secret=' + EncodingUtil.urlEncode(clientSecret, 'UTF-8') +
'&username=' + EncodingUtil.urlEncode(username, 'UTF-8') +
'&password=' + EncodingUtil.urlEncode(password, 'UTF-8');
*/
String requestBody = 'grant_type=password' +
'&client_id=' +clientId+
'&client_secret=' +clientSecret+
'&username=' +username+
'&password=' +password;
request.setBody(requestBody);
System.debug(' =====> Reqest Body : '+requestBody);
// Send the HTTP request
response = http.send(request);
System.debug(' =====> Response Body : '+response.getBody());
// Check the response status and handle accordingly
if (response.getStatusCode() == 200) {
Map<String, Object> responseBody = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
if (responseBody.containsKey('access_token')) {
String accessToken = (String)responseBody.get('access_token');
System.debug('Access Token: ' + accessToken);
return accessToken;
} else {
System.debug('Response did not contain an access token.');
}
} else {
System.debug('Error: HTTP Status ' + response.getStatusCode() + ' | Response: ' + response.getBody());
}
} catch (Exception e) {
System.debug('Exception during HTTP callout: ' + e.getMessage());
}
return null;
}
public static void getMethodCallout(){
HttpRequest req = new HttpRequest();
req.setEndPoint('https://dns000000u40v2aa-dev-ed.develop.my.salesforce.com/services/apexrest//Calculator/*');
req.setHeader('Authorization', 'Bearer '+getAccessToken());
req.setHeader('Content-Type','Application/json');
req.setMethod('GET');
Http http = new Http();
HttpResponse resp = http.send(req);
System.debug(' ====> Status Code : '+resp.getStatus());
System.debug(' ====> Status Code : '+resp.getStatusCode());
System.debug(' ====> Body : '+resp.getBody());
}
}