This is the following response after obtaining the auth_token & auth_verifier.Error: obtaining request token: {errors: [ {code: 215, message: 'Bad Authentication data.}]}. Handling this from backend using node.JS, Any other method or Frontend(react) libraries are also appreciated.
Have already tried some popular react libraries and couldn't get any of them to work for twitter. So, following the twitter documentation and trying to handle it from backend using Node.JS. But, unable to get past step 3, where I am trying to obtain access token. Just need to integrate twitter login in my website.
Below is my code
const axios = require('axios');
const OAuth = require('oauth-1.0a');
const consumerKey = 'igGNukzzPE702IdxapH8WcQiU';
const consumerSecret = 'HPUyFhgSuyOYo5Ro0AZzR4QKSb1FxRoGkoGPgmpdJE5F0ApmA7';
const callbackURL = 'https://dev.betterblueperformance.com/twitter-callback';
// Create an OAuth instance
const oauth = OAuth({
consumer: { key: consumerKey, secret: consumerSecret },
signature_method: 'HMAC-SHA1',
hash_function: (baseString, key) =\> require('crypto').createHmac('sha1', key).update(baseString).digest('base64'),
});
// Set up the OAuth data for the request token
const requestData = {
url: 'https://api.twitter.com/oauth/request_token',
method: 'POST',
data: { oauth_callback: encodeURIComponent(callbackURL) },
};
// Generate the OAuth signature
const authorization = oauth.authorize(requestData);
// Make the request to obtain the request token
axios.post(requestData.url, null, {
headers: {
'Authorization': oauth.toHeader(authorization),
'Content-Type': 'application/x-www-form-urlencoded',
},
})
.then(response =\> {
// Parse the response to extract the request token and secret
const responseData = response.data;
const requestToken = responseData.match(/oauth_token=(\[^&\]+)/)\[1\];
const requestTokenSecret = responseData.match(/oauth_token_secret=(\[^&\]+)/)\[1\];
console.log('Request Token:', requestToken);
console.log('Request Token Secret:', requestTokenSecret);
})
.catch(error =\> {
console.error('Error obtaining request token:', error.response ? error.response.data : error.message);
});