• AkhilMandia
  • NEWBIE
  • 0 Points
  • Member since 2016


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
I am working for a client where we are using deep linking between square-up ios app to FSL (Field Service Lightning ios app). So when we deep link to a flow (FSL action). It is asking for additional confirmation from the user.

User-added image

I am trying to remove this additional security popup from the process. Salesforce has provided steps to use public/private to remove this popup.
Following are terminal commands provided by the salesforce to generate a sing token.
https://developer.salesforce.com/docs/atlas.en-us.field_service_dev.meta/field_service_dev/fsl_dev_mobile_deep_linking_schema.htm

openssl ecparam -genkey -name prime256v1 -noout -out private.pem openssl ec -in private.pem -pubout -out public.pem
pbpaste | openssl dgst -sha256 -sign private.pem | openssl base64 | tr '/+' '_-' | tr -d '=' | tr -d '\n' | pbcopy

I am looking for some apex code to replicate the above command in the apex.
Hi,
I am trying to make an Apex callout to Netsuite which is using OAuth1 Authentication. It uses HMAC-SHA256 to generate signature using consumer secret and token secret. 
 
It is giving me Invalid Login Attempt error with 403 Forbidden error code.
 
Here is my Apex code.
 
public static void getResponse(){
        Netsuite_API__mdt netObj = [Select MasterLabel, Consumer_Key__c, Access_Token__c, Consumer_Secret__c, Endpoint_URL__c, Realm__c, Token_Secret__c From Netsuite_API__mdt Where MasterLabel = 'Restlet'];
        
        String content = '{"createdDate1": ["3/4/2021 12:03 pm"], "createdDate2": ["3/4/2021 12:08 pm"], "lastModifiedDate1": ["3/4/2021 12:03 pm"], "lastModifiedDate2": ["3/4/2021 12:08 pm"]}';
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        HttpResponse response = new HttpResponse();
        
        request.setEndpoint(netObj.Endpoint_URL__c);
        request.setBody(content);
        request.setHeader('Content-Type', 'application/json');
        request.setMethod('POST');
        request = Restlet_Integration.signRequest(request, netObj.Consumer_Key__c, netObj.Consumer_Secret__c, netObj.Realm__c, netObj.Access_Token__c, netObj.Token_Secret__c);
        try{
        	response = http.send(request);
            System.debug(response.getBody());
        	System.debug(response.getStatusCode() + response.getStatus());
        }
        catch(System.CalloutException e){
            System.debug(response.toString());
        	System.debug(response.getStatusCode() + response.getStatus());
            System.debug('Callout Error =' + e);
        }
        
    }
	
    public static HttpRequest signRequest(HttpRequest req, String consumerKey, String consumerSecret, String realm, String access_token, String tokenSecret){
        String nonce = String.valueOf(Crypto.getRandomLong());
        String timeStamp = String.valueOf(DateTime.now().getTime() / 1000);
        
        Map<String, String> parameters = new Map<String, String>();
        parameters.put('realm', realm);
        parameters.put('oauth_consumer_key', consumerKey);
        parameters.put('oauth_token', access_token);
        parameters.put('oauth_signature_method', 'HMAC-SHA256');
        parameters.put('oauth_timestamp', timeStamp);
        parameters.put('oauth_nonce', nonce);
        
        String signature = generateSignature(consumerSecret, tokenSecret);
        String header = generateHeader(signature, parameters);
        System.debug('Header =' + header);
        req.setHeader('Authorization', header);
        
        return req;
    }
    
    private static String generateSignature(String consumerSecret, String tokenSecret){
        String nonceKey = String.valueOf(Crypto.getRandomInteger());
        String secretKey = consumerSecret + '&' + tokenSecret;
        Blob sign = Crypto.generateMac('HmacSHA256', Blob.valueOf(nonceKey), Blob.valueOf(secretKey));
        System.debug('Signature =' +  EncodingUtil.convertToHex(sign));
        return EncodingUtil.convertToHex(sign);
    }
    
    private static String generateHeader(String signature, Map<String, String> parameters){
        String header = 'OAuth ';
        for(String key : parameters.keySet()){
            header = header + key + '="' + parameters.get(key) + '", ';
        }
        
        return header + 'oauth_signature="' + signature + '"';
    }

 
trigger CountCont  on contact (after insert, after delete) {
    map<Id, Integer> mapcount = new map<Id, Integer>();
    if(trigger.isInsert) {
        for(contact c : trigger.new) {
            if(c.account != null) {
                if(!mapcount.containsKey(c.account)) {
                    mapcount.put(c.account, 1);
                } else {
                    mapcount.put(c.account, mapcount.get(c.account) + 1);
                }
            }
        }
    } else {
        for(contact c : trigger.old) {
            if(c.account != null) {
                if(!mapcount.containsKey(c.account)) {
                    mapcount.put(c.account, -1);
                } else {
                    mapcount.put(c.account, mapcount.get(c.account) - 1);
                }
            }
        }
    }
    if(mapcount.size() > 0) {
        List<account> a = [SELECT Id, size_of_account__c FROM account WHERE Id IN : mapcount.keySet()];
        
        for(account ac : a) {
            a.size_of_account__c += mapcount.get(a.Id);
        }
        
        update a;
    }
}