function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kezia dolakezia dola 

How to regenerate Bearer token for every 6 days

Hi,

I am generating a bearer token in API. Now, I want to add a condition to regenerate a new bearer token for every 6 days. 
This is my code:
-------------------
public class qcOauth {
    public static string AuthCode(){
        string Authcode;
        qc__c    Qc = qc__c.getvalues('qc details');
        authWrapreq Aw = new authWrapreq();
        Aw.clientId = Qc.Consumer_Key__c;
        Aw.username = Qc.UserName__c;
        Aw.password = Qc.Password__c;
        
        string endpoint = Qc.url__c+'verify';
        HttpRequest request = new HttpRequest();
        request.setEndpoint(endpoint);
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json');
        String jsonBody = JSON.serialize(Aw);
      //  system.debug('JSON BODY : '+jsonBody);
        request.setBody(jsonBody);
        Http http = new Http();
        HttpResponse response = http.send(request);
     //   system.debug('response body: '+response.getBody());
        if(response.getStatusCode() == 200){
            authRes res = (authRes)json.deserialize(response.getBody(), authRes.class);
            authcode = res.authorizationCode;
        }
     //   system.debug(authcode);
        return authcode;
    }
    
    public static string BearerToken(){
        string BearerToken;
        qc__c    Qc = qc__c.getvalues('qc details');
        BearerWrapReq Bw = new BearerWrapReq();
        Bw.clientId = Qc.Consumer_Key__c;
        Bw.clientSecret = Qc.Consumer_secret__c;
        Bw.authorizationCode = Authcode();
        
        string endpoint = Qc.url__c+'token';
        HttpRequest request = new HttpRequest();
        request.setEndpoint(endpoint);
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json');
        String jsonBody = JSON.serialize(Bw);
    //    system.debug('JSON BODY : '+jsonBody);
        request.setBody(jsonBody);
        Http http = new Http();
        HttpResponse response = http.send(request);
     //   system.debug('response body: '+response.getBody());
        if(response.getStatusCode() == 200){
            BearRes res = (BearRes)json.deserialize(response.getBody(), BearRes.class);
            BearerToken = res.token;
        }
     //   system.debug(BearerToken);
        return BearerToken;
    }

    public class authWrapreq{
        public string clientId;
        public string username;
        public string password;
    }
    public class authRes{
        public string authorizationCode;
    }
    public class BearerWrapReq{
        public string clientId;
        public string clientSecret;
        public string authorizationCode;
    }
    public class BearRes{
        public string token;
    }
}

Any Idea how to do this?

Thanks,
Kezia
 
Ashish Singh SFDCAshish Singh SFDC
Hi Kezia,

In custom setting, create a new field for storing access tokens and a date field to store the data you want the next token to be generated

Now in your apex class, in instance block or constructor first check if the custom setting date is smaller than today which means you need to generate a new token. You'll generate a new token and capture your response to your access token in a variable. Now you can make a new web service request since you already have an access token after getting a response, first update Both Access Token and Next Date i.e. Today+6 for token generation. Then do deserialization and the rest of the steps.

Immediately we didn't updated custom setting because before making callout you cannot perform DML operation. This means first server call we made and got access token and then we made another server call to get service and then we updated custom Setting.

This is one way to achieve your solution. 

Thanks,
Ashish Singh.