twitter signature node code example

Example: twitter signature node

const request = require('request');
const crypto = require('crypto')
const OAuth = require('oauth-1.0a')
 

 //Remember
const consumerKey = ''
const consumerSecret = ''
const accessToken = ''

function hash_function_sha1(base_string, key) {
    return crypto
        .createHmac('sha1', key)
        .update(base_string)
        .digest('base64')
}
 
const oauth = OAuth({
    consumer: { key: consumerKey, secret: consumerSecret },
    signature_method: 'HMAC-SHA1',
    hash_function: hash_function_sha1,
});

const request_data = {
    url: 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=realDonaldTrump',
    method: 'GET'
}

 
request(
    {
        url: request_data.url,
        method: request_data.method,
        headers: oauth.toHeader(oauth.authorize(request_data, accessToken)),
        json: true
    },
    function(error, response, body) {
       console.log(body);
    }
)