Skip to main content
deleted 32 characters in body
Source Link
Sachin Hooda
  • 1.7k
  • 9
  • 12
@AuraEnabled public static String validateAuthToken_Apex(){
        GDrive__c gdAuth = new GDrive__c(); //custom setting
        gdAuth = GDrive__c.getorgDefaults();
        if(gdAuth != null){
            DateTime curTime = System.now();
            if(gdAuth.Access_Token__c == '' || gdAuth.Access_Token__c == null){
                //inserts access token & set expiry time of token
                return 'required';
            }
            else{
                if(curTime < gdAuth.Expires_at__c){
                    //Token isnt expired
                return 'notrequired';
                }
                else{
                    ///Request refresh token
                    Boolean response = getRefreshToken_Apex(gdAuth);
                        if(response){
                            return 'refreshed';
                    }
                    else{
                        return 'some error';
                    }
                }
            }
        }
        else{
            return 'false';
        }
    }
@AuraEnabled public static String validateAuthToken_Apex(){
        GDrive__c gdAuth = new GDrive__c(); //custom setting
        gdAuth = GDrive__c.getorgDefaults();
        if(gdAuth != null){
            DateTime curTime = System.now();
            if(gdAuth.Access_Token__c == '' || gdAuth.Access_Token__c == null){
                //inserts access token & set expiry time of token
                return 'required';
            }
            else{
                if(curTime < gdAuth.Expires_at__c){
                    //Token isnt expired
                return 'notrequired';
                }
                else{
                    ///Request refresh token
                    Boolean response = getRefreshToken_Apex(gdAuth);
                        if(response){
                            return 'refreshed';
                    }
                    else{
                        return 'some error';
                    }
                }
            }
        }
        else{
            return 'false';
        }
    }
@AuraEnabled public static String validateAuthToken_Apex(){
        GDrive__c gdAuth = new GDrive__c(); //custom setting
        gdAuth = GDrive__c.getorgDefaults();
        if(gdAuth != null){
            DateTime curTime = System.now();
            if(gdAuth.Access_Token__c == null){
                //inserts access token & set expiry time of token
                return 'required';
            }
            else{
                if(curTime < gdAuth.Expires_at__c){
                    //Token isnt expired
                return 'notrequired';
                }
                else{
                    ///Request refresh token
                    Boolean response = getRefreshToken_Apex(gdAuth);
                        if(response){
                            return 'refreshed';
                    }
                    else{
                        return 'some error';
                    }
                }
            }
        }
        else{
            return 'false';
        }
    }
added 657 characters in body
Source Link
Sachin Hooda
  • 1.7k
  • 9
  • 12

So, you might have got a case and the access token length exceeds 255 characters, Since Custom Setting hasn't got an option to create fields of TEXTAREA(long) which can hold values in that case. So you'd need to create Custom Metadata instead of Custom Setting. Yes, the procedure is pretty same except you'd need a class that implements Metadata.DeployCallback interface. So,

public class CreateUpdateMetadataUtils implements Metadata.DeployCallback {

}

And unlike custom settings, metadata are used with __mdt though the fields would have __c. Once you done creating the Custom Metadata, write your class to populate the fields. Refer here.

So, you might have got a case and the access token length exceeds 255 characters, Since Custom Setting hasn't got an option to create fields of TEXTAREA(long) which can hold values in that case. So you'd need to create Custom Metadata instead of Custom Setting. Yes, the procedure is pretty same except you'd need a class that implements Metadata.DeployCallback interface. So,

public class CreateUpdateMetadataUtils implements Metadata.DeployCallback {

}

And unlike custom settings, metadata are used with __mdt though the fields would have __c. Once you done creating the Custom Metadata, write your class to populate the fields. Refer here.

Source Link
Sachin Hooda
  • 1.7k
  • 9
  • 12

I'd suggest you, creating a Custom Setting for it. Create three fields, Access Token, Refresh Token & expiry time. Before making a callout to the external service, check whether the token you already have in your custom setting is expired or not. If the token has expired, make Callout to the external web service & request a refresh token else use the one, you've stored.

The idea would be similar to this.

@AuraEnabled public static String validateAuthToken_Apex(){
        GDrive__c gdAuth = new GDrive__c(); //custom setting
        gdAuth = GDrive__c.getorgDefaults();
        if(gdAuth != null){
            DateTime curTime = System.now();
            if(gdAuth.Access_Token__c == '' || gdAuth.Access_Token__c == null){
                //inserts access token & set expiry time of token
                return 'required';
            }
            else{
                if(curTime < gdAuth.Expires_at__c){
                    //Token isnt expired
                return 'notrequired';
                }
                else{
                    ///Request refresh token
                    Boolean response = getRefreshToken_Apex(gdAuth);
                        if(response){
                            return 'refreshed';
                    }
                    else{
                        return 'some error';
                    }
                }
            }
        }
        else{
            return 'false';
        }
    }

And while populating the access token or the refresh token in custom settings, populate Expiry time current time + 3600 seconds (as you mentioned the expiry duration)

gdAuth.Expires_at__c = System.now().addSeconds(Integer.ValueOf(gdAuthResp.get('expires_in')));