Skip to content

Instantly share code, notes, and snippets.

@nylen
Created October 28, 2014 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nylen/639ef899d9964cb2957c to your computer and use it in GitHub Desktop.
Save nylen/639ef899d9964cb2957c to your computer and use it in GitHub Desktop.
Test Request's OAuth client implementation
#!/usr/bin/env node
var async = require('async'),
querystring = require('querystring'),
request = require('request');
var requestToken,
accessToken;
async.series([
function(next) {
request({
url : 'http://oauthbin.com/v1/request-token',
oauth : {
consumer_key : 'key',
consumer_secret : 'secret'
}
}, function(err, res, body) {
if (err) throw err;
requestToken = querystring.parse(body);
if (!requestToken.oauth_token) {
throw new Error(body);
}
console.log('got request token', requestToken);
next();
});
},
function(next) {
request({
url : 'http://oauthbin.com/v1/access-token',
oauth : {
consumer_key : 'key',
consumer_secret : 'secret',
token : requestToken.oauth_token,
token_secret : requestToken.oauth_token_secret
}
}, function(err, res, body) {
if (err) throw err;
accessToken = querystring.parse(body);
if (!accessToken.oauth_token) {
throw new Error(body);
}
console.log('got access token', accessToken);
next();
});
},
function(next) {
request({
url : 'http://oauthbin.com/v1/echo?mars=barsoom',
oauth : {
consumer_key : 'key',
consumer_secret : 'secret',
token : accessToken.oauth_token,
token_secret : accessToken.oauth_token_secret
}
}, function(err, res, body) {
if (err) throw err;
if (body != 'mars=barsoom') {
throw new Error(body);
}
console.log('made request', body);
next();
});
}
]);
#!/usr/bin/env node
var async = require('async'),
querystring = require('querystring'),
request = require('request');
// private key from http://oauthbin.com/?sig_method=RSA-SHA1
var rsaPrivateKey = [
'-----BEGIN PRIVATE KEY-----',
'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALRiMLAh9iimur8V',
'A7qVvdqxevEuUkW4K+2KdMXmnQbG9Aa7k7eBjK1S+0LYmVjPKlJGNXHDGuy5Fw/d',
'7rjVJ0BLB+ubPK8iA/Tw3hLQgXMRRGRXXCn8ikfuQfjUS1uZSatdLB81mydBETlJ',
'hI6GH4twrbDJCR2Bwy/XWXgqgGRzAgMBAAECgYBYWVtleUzavkbrPjy0T5FMou8H',
'X9u2AC2ry8vD/l7cqedtwMPp9k7TubgNFo+NGvKsl2ynyprOZR1xjQ7WgrgVB+mm',
'uScOM/5HVceFuGRDhYTCObE+y1kxRloNYXnx3ei1zbeYLPCHdhxRYW7T0qcynNmw',
'rn05/KO2RLjgQNalsQJBANeA3Q4Nugqy4QBUCEC09SqylT2K9FrrItqL2QKc9v0Z',
'zO2uwllCbg0dwpVuYPYXYvikNHHg+aCWF+VXsb9rpPsCQQDWR9TT4ORdzoj+Nccn',
'qkMsDmzt0EfNaAOwHOmVJ2RVBspPcxt5iN4HI7HNeG6U5YsFBb+/GZbgfBT3kpNG',
'WPTpAkBI+gFhjfJvRw38n3g/+UeAkwMI2TJQS4n8+hid0uus3/zOjDySH3XHCUno',
'cn1xOJAyZODBo47E+67R4jV1/gzbAkEAklJaspRPXP877NssM5nAZMU0/O/NGCZ+',
'3jPgDUno6WbJn5cqm8MqWhW1xGkImgRk+fkDBquiq4gPiT898jusgQJAd5Zrr6Q8',
'AO/0isr/3aa6O6NLQxISLKcPDk2NOccAfS/xOtfOz4sJYM3+Bs4Io9+dZGSDCA54',
'Lw03eHTNQghS0A==',
'-----END PRIVATE KEY-----'
].join('\n');
var requestToken,
accessToken;
async.series([
function(next) {
request({
url : 'http://oauthbin.com/v1/request-token',
oauth : {
signature_method : 'RSA-SHA1',
private_key : rsaPrivateKey,
consumer_key : 'key'
}
}, function(err, res, body) {
if (err) throw err;
requestToken = querystring.parse(body);
if (!requestToken.oauth_token) {
throw new Error(body);
}
console.log('got request token', requestToken);
next();
});
},
function(next) {
request({
url : 'http://oauthbin.com/v1/access-token',
oauth : {
signature_method : 'RSA-SHA1',
private_key : rsaPrivateKey,
consumer_key : 'key',
token : requestToken.oauth_token,
token_secret : requestToken.oauth_token_secret
}
}, function(err, res, body) {
if (err) throw err;
accessToken = querystring.parse(body);
if (!accessToken.oauth_token) {
throw new Error(body);
}
console.log('got access token', accessToken);
next();
});
},
function(next) {
request({
url : 'http://oauthbin.com/v1/echo?mars=barsoom',
oauth : {
signature_method : 'RSA-SHA1',
private_key : rsaPrivateKey,
consumer_key : 'key',
token : accessToken.oauth_token,
token_secret : accessToken.oauth_token_secret
}
}, function(err, res, body) {
if (err) throw err;
if (body != 'mars=barsoom') {
throw new Error(body);
}
console.log('made request', body);
next();
});
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment