Skip to content

Instantly share code, notes, and snippets.

@brien-crean
Created July 14, 2017 18:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brien-crean/0967f387098d1d6af94ec7ae1eb7a9d5 to your computer and use it in GitHub Desktop.
Save brien-crean/0967f387098d1d6af94ec7ae1eb7a9d5 to your computer and use it in GitHub Desktop.
Twitter 3 legged OAuth on AWS Lambda with NodeJS
const OAuth = require('oauth');
const Twit = require('twit');
const nconf = require('nconf');
// temp global var
var oAuthTokenSecret;
// load config file which contains twitter app tokens
nconf.file({ file: './config/twitter-share.json' }).env()
// request user OAuth
const oauth = new OAuth.OAuth(
'https://api.twitter.com/oauth/request_token',
'https://api.twitter.com/oauth/access_token',
nconf.get('consumer_key'),
nconf.get('consumer_secret'),
'1.0',
//API ENDPOINT URL FOR oauthResponse LAMBDA BELOW e.g.
' https://xxxxxxxx.execute-api.xx-xxxx.amazonaws.com/dev/twitter/oauthResponse',
'HMAC-SHA1'
);
// request app OAuth
const createT = (oAuthAccessToken, oAuthAccessTokenSecret) => new Twit({
consumer_key: nconf.get('consumer_key'),
consumer_secret: nconf.get('consumer_secret'),
access_token: oAuthAccessToken,
access_token_secret: oAuthAccessTokenSecret
});
module.exports.oauthGET = (event, context, callback) => {
oauth.getOAuthRequestToken((err, OAuthToken, OAuthTokenSecret, results) => {
oAuthTokenSecret = OAuthTokenSecret;
let html = [
'<a href="https://api.twitter.com/oauth/authenticate?oauth_token=',
OAuthToken,
'">',
'Sign in with twitter</a>'
].join('');
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
},
body: html
};
callback(null, response);
});
}
module.exports.oauthResponse = (event, context, callback) => {
console.log(event);
oauth.getOAuthAccessToken(
event.query.oauth_token,
oAuthTokenSecret,
event.query.oauth_verifier,
(err, oAuthAccessToken, oAuthAccessTokenSecret, results) => {
let T = createT(oAuthAccessToken, oAuthAccessTokenSecret);
T.post('statuses/update', { status: 'testing 3 Legs auth' }, (err, data, response) => {
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment