+ Start a Discussion
Dhilip DussaDhilip Dussa 

Help me on Twitter Integration

Good Morning,

I'm facing one issue on Twitter integration

Error: {"errors":[{"code":32,"message":"Could not authenticate you."}]}

Request: curl --request POST --url 'https://api.twitter.com/1.1/friendships/create.json?user_id=2244994945&follow=true' --header 'authorization: OAuth oauth_consumer_key="YOUR_CONSUMER_KEY", oauth_nonce="AUTO_GENERATED_NONCE", oauth_signature="AUTO_GENERATED_SIGNATURE", oauth_signature_method="HMAC-SHA1", oauth_timestamp="AUTO_GENERATED_TIMESTAMP", oauth_token="USERS_ACCESS_TOKEN", oauth_version="1.0"' --header 'content-type: application/json'

My Code:

    public Static void followUser(){
        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];
        String oathNonce = TwitterController.generateOathNonce();
        Long timestamp = TwitterController.generateTimeStamp();
        String signature = '';
        String othSecret = Encodingutil.urlEncode(twitter.Access_Token_Secret__c, 'UTF-8');
        String consumSecret = Encodingutil.urlEncode(twitter.API_Secret_Key__c, 'UTF-8');
        String othToken = Encodingutil.urlEncode(twitter.Access_Token__c, 'UTF-8');
        String othNonce = Encodingutil.urlEncode(oathNonce, 'UTF-8');
        String consumKey = Encodingutil.urlEncode(twitter.API_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/friendships/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 );        
        signature =  EncodingUtil.base64Encode(signBlob);
        System.debug('signature=====>>>>>>>'+signature);
        if(String.isNotBlank(signature))
        {
            signature = Encodingutil.urlEncode(signature, 'UTF-8');
            HttpRequest req = new HttpRequest();
            req.setEndpoint('https://api.twitter.com/1.1/friendships/create.json?screen_name=s_cheera&follow=true');
            req.setMethod('POST');
            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"';
            system.debug('### req String :'+reqstring);
            req.setHeader('Authorization',reqstring);
            req.setHeader('Content-Type','application/json');
           // String bearerAuthorizationHeader='Bearer '  + accessToken;
            //req.setHeader('Authorization',bearerAuthorizationHeader);
            Http http = new Http();
            HTTPResponse res = http.send(req);
            system.debug(res.getBody());            
        }
    }

please help me.

Thanks,
Dhilip Dussa.
SwethaSwetha (Salesforce Developers) 
HI Dhilip,
Recommend trying the steps mentioned in the article https://developer.twitter.com/en/support/twitter-api/error-troubleshooting - Refer section "Code 32 - Could not authenticate you" section

If this information helps, please mark the answer as best. Thank you