Reputation: 3358
I'm trying to Implement Twitter login in my NodeJS app.
As per the documentation from Twitter, I need to pass the below parameters to this url via POST request. URL:
https://api.twitter.com/oauth/request_token
PARAMETERS:
oauth_nonce=,
oauth_signature=,
oauth_callback="http%3A%2F%2Fmyapp.com%3A3005%2Ftwitter%2Fprocess_callback",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="currentTimestamp",
oauth_consumer_key=“myKEY”,
oauth_version="1.0"
I'm passing a random string for oauth_nonce
parameter. I'm not clear on how to create a oauth_signature
?
I keep getting 215 error whenever I make the POST request beacuse of incorrect oauth_nonce
and oauth_signature
values I guess.
How do I generate oauth_nonce
and oauth_signature
values while making the request in NodeJS.
Upvotes: 6
Views: 1496
Reputation: 9923
To answer your question:
How do I generate oauth_nonce and oauth_signature values while making the request in NodeJS.
Going through these post will be of help to you Creating a signature and How to generate an OAuth nonce.
Twitter makes a whole lot of sense if used correctly. I have seen some tutorials that has been quite useful to those around me, that I feel maybe you might want to check Node Authentication: Twitter and Implementing Sign in with Twitter for Node.js
Or you might want to check passportjs
You will have to install it in your project by running:
npm install passport-twitter
const passport = require('passport')
, TwitterStrategy = require('passport-twitter').Strategy;
passport.use(new TwitterStrategy({
consumerKey: TWITTER_CONSUMER_KEY,
consumerSecret: TWITTER_CONSUMER_SECRET,
callbackURL: "http://www.example.com/auth/twitter/callback"
},
function(token, tokenSecret, profile, done) {
User.findOrCreate(..., function(err, user) {
if (err) { return done(err); }
done(null, user);
});
}
));
This Github repo might be a great reference to you.
If you're using reactjs, this (npm i react-twitter-auth) might be useful to you too.
Upvotes: 1
Reputation: 1859
Those parameters need to be passed in your authorization header:
OAuth oauth_nonce="K7ny27JTpKVsTgdyLdDfmQQWVLERj2zAK5BslRsqyw",
oauth_callback="http%3A%2F%2Fmyapp.com%3A3005%2Ftwitter%2Fprocess_callback",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1300228849",
oauth_consumer_key="OqEqJeafRSF11jBMStrZz",
oauth_signature="Pc%2BMLdv028fxCErFyi8KXFM%2BddU%3D",
oauth_version="1.0"
But before that, you need to get a signature, which will then give you all of the parameters above. Check the documentation here.
A recommendation would be to use a 3rd party library like passport, which heavily simplifies this process if needed.
Upvotes: 5