Skip to content

Instantly share code, notes, and snippets.

@adityamenon
Forked from PhilKershaw/gist:2171814
Last active January 4, 2016 16:32
Show Gist options
  • Save adityamenon/768540f7d47491b17ac5 to your computer and use it in GitHub Desktop.
Save adityamenon/768540f7d47491b17ac5 to your computer and use it in GitHub Desktop.
Simple example of using 2-legged OAuth in Node.js (requires node-oauth)
var express = require('express');
var oauth = require('oauth');
var sys = require('sys');
var app = express.createServer();
var key = "[api-key]";
var secret = "[api-secret]";
var request = new oauth.OAuth(null, null, key, secret, '1.0', null, 'HMAC-SHA1');
app.get('/api-request', function(req, res){
request.get(
"http://127.0.0.1:8000/api/1.0/data.json",
null,
null,
function (err, data, result) {
if (err) {
res.send("Error getting data : " + sys.inspect(err), 500);
} else {
res.send(data)
}
});
});
app.listen(parseInt(process.env.PORT || 88));
{
"name": "node_oauth",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^2.5.10",
"oauth": "^0.9.14",
"sys": "0.0.1"
}
}
@adityamenon
Copy link
Author

Added stuff to make sure it all works with the latest node as of this writing (v5.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment