Skip to content

Instantly share code, notes, and snippets.

@triptych
Forked from JuanJo4/app.js
Created September 26, 2020 21:53
Show Gist options
  • Save triptych/d82a8084645bd98809c2985b27429ce7 to your computer and use it in GitHub Desktop.
Save triptych/d82a8084645bd98809c2985b27429ce7 to your computer and use it in GitHub Desktop.
Twitter OAuth with node-oauth for node.js + express 4
/*
Node.js, express, oauth example using Twitters API
Install Dependencies:
npm install express
npm install oauth
Create App File:
Save this file to app.js
Start Server:
node app.js
Navigate to the page:
Local host: http://127.0.0.1:8080
Remote host: http://yourserver.com:8080
*/
var express = require('express');
var bodyParser = require('body-parser');
var logger = require('express-logger');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var inspect = require('util-inspect');
var oauth = require('oauth');
var app = express();
// Get your credentials here: https://dev.twitter.com/apps
var _twitterConsumerKey = "twitterConsumerKey";
var _twitterConsumerSecret = "twitterConsumerSecret";
var consumer = new oauth.OAuth(
"https://twitter.com/oauth/request_token", "https://twitter.com/oauth/access_token",
_twitterConsumerKey, _twitterConsumerSecret, "1.0A", "http://127.0.0.1:8080/sessions/callback", "HMAC-SHA1");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(logger({ path: "log/express.log"}));
app.use(cookieParser());
app.use(session({ secret: "very secret", resave: false, saveUninitialized: true}));
app.use(function(req, res, next) {
res.locals.session = req.session;
next();
});
app.get('/sessions/connect', function(req, res){
consumer.getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret, results){
if (error) {
res.send("Error getting OAuth request token : " + inspect(error), 500);
} else {
req.session.oauthRequestToken = oauthToken;
req.session.oauthRequestTokenSecret = oauthTokenSecret;
console.log("Double check on 2nd step");
console.log("------------------------");
console.log("<<"+req.session.oauthRequestToken);
console.log("<<"+req.session.oauthRequestTokenSecret);
res.redirect("https://twitter.com/oauth/authorize?oauth_token="+req.session.oauthRequestToken);
}
});
});
app.get('/sessions/callback', function(req, res){
console.log("------------------------");
console.log(">>"+req.session.oauthRequestToken);
console.log(">>"+req.session.oauthRequestTokenSecret);
console.log(">>"+req.query.oauth_verifier);
consumer.getOAuthAccessToken(req.session.oauthRequestToken, req.session.oauthRequestTokenSecret, req.query.oauth_verifier, function(error, oauthAccessToken, oauthAccessTokenSecret, results) {
if (error) {
res.send("Error getting OAuth access token : " + inspect(error) + "[" + oauthAccessToken + "]" + "[" + oauthAccessTokenSecret + "]" + "[" + inspect(result) + "]", 500);
} else {
req.session.oauthAccessToken = oauthAccessToken;
req.session.oauthAccessTokenSecret = oauthAccessTokenSecret;
res.redirect('/home');
}
});
});
app.get('/home', function(req, res){
consumer.get("https://api.twitter.com/1.1/account/verify_credentials.json", req.session.oauthAccessToken, req.session.oauthAccessTokenSecret, function (error, data, response) {
if (error) {
//console.log(error)
res.redirect('/sessions/connect');
} else {
var parsedData = JSON.parse(data);
res.send('You are signed in: ' + inspect(parsedData.screen_name));
}
});
});
app.get('*', function(req, res){
res.redirect('/home');
});
app.listen(8080, function() {
console.log('App runining on port 8080!');
});
{
"name": "twitter-oauth",
"version": "1.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"body-parser": "^1.15.2",
"cookie-parser": "^1.4.3",
"express": "^4.14.0",
"express-logger": "0.0.3",
"express-session": "^1.14.1",
"oauth": "^0.9.14",
"util-inspect": "^0.1.8"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Juan Gonzalez <juan@kukenan.tech>",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment