Last active
February 27, 2019 09:39
-
-
Save osule/5ea0a673c0ed05c2ceaf178f0d12aaba to your computer and use it in GitHub Desktop.
3 Leg POC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "gists", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"@request/headers": "request/headers", | |
"consolidate": "^0.14.5", | |
"cookie-parser": "^1.4.3", | |
"express": "^4.15.3", | |
"hogan": "^1.0.2", | |
"node-fetch": "^1.7.1", | |
"oauth": "^0.9.15", | |
"twit": "^2.2.9", | |
"url-search-params": "^0.9.0" | |
}, | |
"scripts": { | |
"start": "node twitter.js" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const engines = require('consolidate'); | |
const OAuth = require('oauth'); | |
const Twit = require('twit'); | |
const cookieParser = require('cookie-parser') | |
const app = express(); | |
app.use(cookieParser()); | |
const oauth = new OAuth.OAuth( | |
'https://api.twitter.com/oauth/request_token', | |
'https://api.twitter.com/oauth/access_token', | |
'<consumer_key>', | |
'<consumer_secret>', | |
'1.0', | |
'http://localhost:3000/post_auth', | |
'HMAC-SHA1', | |
); | |
const createT = (oAuthAccessToken, oAuthAccessTokenSecret) => new Twit({ | |
consumer_key: '<consumer_key>', | |
consumer_secret: '<consumer_secret>', | |
access_token: oAuthAccessToken, | |
access_token_secret: oAuthAccessTokenSecret | |
}); | |
app.get('/', (req, res) => { | |
oauth.getOAuthRequestToken((err, OAuthToken, OAuthTokenSecret, results) => { | |
app.locals.oAuthTokenSecret = OAuthTokenSecret; | |
res.send([ | |
'<a href="https://api.twitter.com/oauth/authenticate?oauth_token=', | |
OAuthToken, | |
'">', | |
'Sign in with twitter</a>' | |
].join('')); | |
}); | |
}); | |
app.get('/post_auth', (req, res) => { | |
console.log(app.locals.oAuthTokenSecret); | |
oauth.getOAuthAccessToken( | |
req.query.oauth_token, | |
app.locals.oAuthTokenSecret, | |
req.query.oauth_verifier, | |
(err, oAuthAccessToken, oAuthAccessTokenSecret, results) => { | |
console.log(oAuthAccessToken, oAuthAccessTokenSecret); | |
let T = createT(oAuthAccessToken, oAuthAccessTokenSecret); | |
T.post('statuses/update', { status: 'testing 3 Legs auth' }, (err, data, response) => { | |
console.log(data); | |
}); | |
}); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment