Twitter request token from salesforce

restapi

#1

I am trying to get request token from my salesforce developer account. But I am getting 401 status error on running the code.

public void makeCall(){
        String callBack = EncodingUtil.urlEncode('https://login.salesforce.com/','UTF-8');
        String consumerKey = 'XXXXX';
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        String timestamp = String.valueOf(DateTime.now().getTime()/1000);
        String nounce = String.valueOf(Crypto.getRandomLong());
        //String nounce = 'kYjzVBB8Y0ZFabxSWb345Y3uYSQ2pTgmZeNu2VS4cg';
        system.debug(timestamp+'   '+nounce);
        String paramString = EncodingUtil.urlencode('oauth_callback','UTF-8')+'='+EncodingUtil.urlencode(callBack,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_consumer_key','UTF-8')+'='+EncodingUtil.urlencode(consumerKey,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_nonce','UTF-8')+'='+EncodingUtil.urlencode(nounce,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_signature_method','UTF-8')+'='+EncodingUtil.urlencode('HMAC-SHA1','UTF-8')+'&'+EncodingUtil.urlencode('oauth_timestamp','UTF-8')+'='+EncodingUtil.urlencode(timestamp,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_version','UTF-8')+'='+EncodingUtil.urlencode('1.0','UTF-8');
        system.debug('#####param string '+paramString);
        req.setEndpoint('https://api.twitter.com/oauth/request_token');
        String basestring = 'POST'+'&'+EncodingUtil.urlencode('https://api.twitter.com/oauth/request_token','UTF-8')+'&'+EncodingUtil.urlencode(paramString,'UTF-8');
        system.debug('**basestring '+basestring);
        String signkey = EncodingUtil.urlencode('ZZZZZ','UTF-8')+'&';
        system.debug('***sign key '+signKey);
        Blob OAuth = Crypto.generateMac('hmacSHA1' , Blob.valueOf(baseString) , blob.valueOf(signkey));
        String final_signature = EncodingUtil.urlEncode(EncodingUtil.base64Encode(Oauth), 'UTF-8');
        //string authheader = 'OAuth oauth_callback='+callBack+',oauth_nonce='+nounce+',oauth_signature='+final_signature+',oauth_signature_method="HMAC-SHA1",oauth_timestamp='+timestamp+',oauth_consumer_key='+consumerKey+',oauth_version="1.0"' ;
        String authheader = 'OAuth '+'oauth_callback'+'='+'"'+callBack+'"'+', '+'oauth_nonce'+'='+'"'+nounce+'"'+', '+'oauth_signature'+'='+'"'+final_signature+'"'+', '+'oauth_signature_method'+'='+'"'+'HMAC-SHA1'+'"'+', '+'oauth_timestamp'+'='+'"'+timestamp+'"'+', '+'oauth_consumer_key'+'='+'"'+consumerKey+'"'+', '+'oauth_version'+'='+'"'+'1.0'+'"';
        system.debug('#### authheader '+authheader);
        req.setMethod('POST');
        req.setHeader('Authorization', authheader);
        HttpResponse res = h.send(req);
        system.debug(res);
    }

XXX being the consumer key and ZZZ being the consumer secret