1

I have a requirement where I need to make a callout to an external webservice. However, to make the callout, I need to get a OpenId Auth Token from a corporate webservice first. So, I am trying to use platform cache where I can cache the Auth Token. If the Cache has the token, I can use it directly. If not, I need to make a callout to the OpenId Auth Service to get the token and with that token I can make a callout to the external webservice.

Below is my code.

    public static String getAuthToken() {

  String strJWToken = (String)Cache.Org.get('local.OIDC.OneToken');
  if(String.isNotEmpty(strJWToken)) {
      System.debug('## No need to fetch the OIDC token. Current Token = ' + strJWToken); 
      return strJWToken;
  }
  else {     
      Http http = new Http();
      HttpRequest httpReq = new HttpRequest();
      String strTokenType;
      Integer expiresIn;  
      httpReq.setEndpoint('callout:OpenIDConnect'+endPoint.Service_Method__c);
      httpReq.setTimeout(60000);
      httpReq.setMethod('POST');
      httpReq.setHeader('X-IBM-Client-Id', strClientId); 
      String username = '{!$Credential.UserName}';
      String password = '{!$Credential.Password}';
      
      
      httpReq.setBody('grant_type=password&scope=openid+profile+address+email&username='+username+'&password='+password);
      
      // Make the webservice call and store the response
      HttpResponse httpResp = new Http().send(httpReq);
      
      
      // Parse the response to get the access token
      System.JSONParser parser = JSON.createParser(httpResp.getBody());         
      while (parser.nextToken() != null) {
          if(parser.getCurrentToken()== JSONToken.FIELD_NAME){
                String fieldName = parser.getText();
                parser.nextToken();
                if(fieldName == 'access_token'){
                  strJWToken = parser.getText();
                } else if(fieldName == 'expires_in') {
                   expiresIn = parser.getIntegerValue(); 
                } else if(fieldName == 'token_type') {
                   strTokenType = parser.getText();  
                }
          }
       }
       System.debug('@@ Partition Name:- '+Cache.Org.getName() +' - '+ Cache.Org.getPartition(Cache.Org.getName()) + ' - Token from Cache = ' + strJWToken);
       Cache.Org.put('local.OIDC.OneToken', strJWToken, 1500, Cache.Visibility.ALL, false);
       return strJWToken;

}

In the main class, I making a call to this method with the statement below.

        String strOpenIdToken = Util.getAuthToken();                            // 5.0

However, when this statement gets executed, I am seeing the below error message.

Partition.DEFAULT_NAME=default, Partition.DELIMITER=., Partition.FULL_DEFAULT_PARTITION=c.default, Partition.INVALID_CACHE_BUILDER= does not implement CacheBuilder, Partition.INVALID_KEY=Key cannot be null or empty and must be alphanumeric, Partition.INVALID_NAMESPACE=Namespace cannot be null or empty, Partition.INVALID_PARTITION=Partition name cannot be null or empty and must be alphanumeric, Partition.INVALID_VALUE=Value cannot be null, delegate=PlatformCacheDelegateWrapper:[delegate=common.apex.api.platformcache.OrgCacheDelegate@7c623317]] 14:34:43.0 (10419967)|USER_DEBUG|[712]|INFO|[Util][getAuthToken] Http Request Body : grant_type=password&scope=openid+profile+address+email&username={!$Credential.UserName}&password={!$Credential.Password}

My platform Cache is setup as shown below.

enter image description here

Can someone help? I am not sure why my partition is throwing so many errors.

Thanks in advance.

2
  • Are you sure this is the method you're calling? Your debug log says Util.getAuthToken is being called but your question has Util.getOIDCBearerToken()
    – identigral
    Oct 6, 2020 at 20:06
  • 1
    Thanks for pointing out. It was a copy-paste issue and I have corrected it. Oct 6, 2020 at 20:08

1 Answer 1

1

The text you're seeing in the debug log is not an error message, it's a stringified representation of the cache partition object.

Anonymously executing

System.debug('Default partition: ' + Cache.Org.getPartition(Cache.Org.getName()));

from an org with a default org-level cache partition yields this log message:

00:22:00:008 USER_DEBUG [1]|DEBUG|Default partition: OrgPartition:[Partition.CURRENT_NAMESPACE=c, Partition.DEFAULT_NAME=default, Partition.DELIMITER=., Partition.FULL_DEFAULT_PARTITION=c.default, Partition.INVALID_CACHE_BUILDER= does not implement CacheBuilder, Partition.INVALID_KEY=Key cannot be null or empty and must be alphanumeric, Partition.INVALID_NAMESPACE=Namespace cannot be null or empty, Partition.INVALID_PARTITION=Partition name cannot be null or empty and must be alphanumeric, Partition.INVALID_VALUE=Value cannot be null, del
Partition.INVALID_CACHE_BUILDER= does not implement CacheBuilder, Partition.INVALID_KEY=Key cannot be null or empty and must be alphanumeric, Partition.INVALID_NAMESPACE=Namespace cannot be null or empty, Partition.INVALID_PARTITION=Partition name cannot be null or empty and must be alphanumeric, Partition.INVALID_VALUE=Value cannot be null, del
3
  • So, this is not an error and I can ignore this? Oct 6, 2020 at 20:47
  • 1
    Insofar as your call to System.debug(...) that produces this log message, yes, you can ignore it.
    – identigral
    Oct 6, 2020 at 20:58
  • Thanks, that helps. Oct 6, 2020 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .