+ Start a Discussion
Dhilip DussaDhilip Dussa 

Twitter Integration facing error in Post Method

Good Morning,

I'm integrating Salesforce and Twitter. I'm getting an error {"errors":[{"code":215,"message":"Bad Authentication data."}]}

https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-favorites-create

Please check code once

public class TwitterController {    
    
    public static String generateOathNonce(){
        Blob blobKey = crypto.generateAesKey(256);
        String key = EncodingUtil.convertToHex(blobKey);
        String oathNonce = EncodingUtil.base64Encode(Blob.valueOf(key));
        oathNonce = oathNonce.replaceAll('[^a-zA-Z0-9]+',''+integer.valueOf(math.random() * 10));
        oathNonce = oathNonce.substring(0,42);
        return oathNonce;
    }
    
    public static long generateTimeStamp(){
        long timestamp = datetime.now().getTime(); 
        timestamp = timestamp / 1000;
        return timestamp;
    }
}

============================================

public class TwitterTweetsCtrl {
public Static void createFavorites(){
        String accessToken = TwitterController.retrieveAccessToken();
        Twitter_API_Settings__c twitter = [SELECT Id, API_Key__c, API_Secret_Key__c, Access_Token__c, Access_Token_Secret__c 
                                           FROM Twitter_API_Settings__c LIMIT 1];
        Long timestamp = TwitterController.generateTimeStamp();
        String oathNonce = TwitterController.generateOathNonce();
        
        String consumKey = Encodingutil.urlEncode(twitter.API_Key__c, 'UTF-8');
        String othNonce = Encodingutil.urlEncode(oathNonce, 'UTF-8');

        String othToken = Encodingutil.urlEncode(twitter.Access_Token__c, 'UTF-8');
        String othSecret = Encodingutil.urlEncode(twitter.Access_Token_Secret__c, 'UTF-8');
        String consumSecret = Encodingutil.urlEncode(twitter.API_Secret_Key__c, 'UTF-8');
        String paramString = 'include_entities=true&'+
            'oauth_consumer_key='+consumKey+'&'+
            'oauth_nonce='+othNonce+'&'+
            'oauth_signature_method=HMAC-SHA1&'+
            'oauth_timestamp='+timestamp+'&'+
            'oauth_token='+othToken+'&'+
            'oauth_version=1.0';
        string baseString = 'POST&'+EncodingUtil.urlEncode('https://api.twitter.com/1.1/favorites/create.json', 'UTF-8')+'&'+ 
            EncodingUtil.urlEncode(paramString, 'UTF-8');

        string signString = consumSecret+'&'+othSecret;
        blob blobBaseString = Blob.ValueOf(baseString);
        blob blobSignString = Blob.ValueOf(signString);
        blob signBlob = crypto.generateMac('hmacSHA1',blobBaseString ,blobSignString );        
        String signature =  EncodingUtil.base64Encode(signBlob);
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint('https://api.twitter.com/1.1/favorites/create.json?id=1412999372476981248');
        string reqstring = 'OAuth oauth_consumer_key="'+consumKey+'",'+
                'oauth_nonce="'+othNonce+'",'+
                'oauth_signature="'+signature +'",'+
                'oauth_signature_method="HMAC-SHA1",'+
                'oauth_timestamp="'+timestamp+'",'+
                'oauth_token="'+othToken+'",'+
                'oauth_version="1.0"';
        req.setHeader('Authorization',reqstring);
        req.setHeader('Content-Type','application/json');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        system.debug(res.getBody()); 
    }
}

please help me,

Thanks,
Dhilip Dussa.

SwethaSwetha (Salesforce Developers) 
Duplicate of https://developer.salesforce.com/forums/ForumsMain?id=9062I000000DN8gQAG