I doing a Apex Callout to the third party API which uses the OAuth. I am beginner in the integration from Salesforce to external systems. I am successfully able to call the third party API and perform GET/POST methods.
@future (callout=true)
public static void createSegmentD365() {
try{
String clientId = 'xxxxxxx';
String clientSecret = 'xxxxxxxx';
String tenant_id = 'xxxxxxxxxxx';
String resource = 'xxxxxxxxxxxx';
String reqbody = 'grant_type=client_credentials&client_id='+clientId+'&client_secret='+clientSecret+'&tenant_id='+tenant_id+'&resource='+resource;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://login.microsoftonline.com/xxxx/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
String bearerToken = resp1.access_token;
Http http1 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setEndpoint('https://xyz/data/parentAccounts');
req1.setMethod('GET');
req1.setHeader('Authorization','Bearer '+bearerToken);
HttpResponse res1 = http1.send(req1);
The access token I retrieved from the Authorization provider is valid for 1 hour. I am trying to understand is there a way where I can cache the access token, so I can use the cached token instead of calling the authprovider again within the 1 hour for Access Token. we wont be able to use the named credentials and looks like custom settings is deprecated. I started creating a custom metadatatype with the following fields
I am not able follow in any of the article how I store the credentials or how I cache the access token / refresh if the token is expired from the custom metadata. Any help greatly appreciated.