Skip to content

Instantly share code, notes, and snippets.

@mzalewski
Forked from kadamwhite/oauth-node-oauth.js
Last active October 26, 2016 07:53
Show Gist options
  • Save mzalewski/2b2ff2de0d88a5c3194c407b796695df to your computer and use it in GitHub Desktop.
Save mzalewski/2b2ff2de0d88a5c3194c407b796695df to your computer and use it in GitHub Desktop.
'use strict';
var oobManualAuthorization = require('./user-authorization-manual');
var OAuth = require( 'oauth' );
var oauth = new OAuth.OAuth(
// reqURL
'http://wpapi.loc/oauth1/request',
// accessURL
'http://wpapi.loc/oauth1/access',
// Key
'OL5EIwSTQyPr',
// Secret
'YDBGBezQPDd51DwDIDhBfrYeSOUJqCQwcHwRnVYebGAmFtU1',
// Version
'1.0A',
// authorize_callback (null in example)
'oob',
// Signature method
'HMAC-SHA1'
// nonceSize
// customHeaders
);
// console.log( auth );
function getRequestToken() {
return new Promise( ( resolve, reject ) => {
oauth.getOAuthRequestToken(function( err, token, secret, results ) {
if ( err ) {
return reject( err );
}
console.log( results );
resolve({
token: token,
secret: secret
});
});
});
}
function getAccessToken(config) {
return new Promise( ( resolve, reject ) => {
oauth.getOAuthAccessToken( config.token, config.secret, config.verifier, function( err, token, secret, results ) {
if ( err ) {
return reject( err );
}
resolve({
token: token,
secret: secret
});
});
});
}
getRequestToken()
.then(oobManualAuthorization)
.then(function( config ) {
console.log( config );
return getAccessToken( config );
})
.then(function( result ) {
console.log( result );
})
.catch( err => console.error( err ) );
'use strict';
var fetch = require( 'isomorphic-fetch' );
var qs = require( 'qs' );
var OAuth = require( 'oauth-1.0a' );
var oauth = new OAuth({
consumer: {
public: 'OL5EIwSTQyPr',
secret: 'YDBGBezQPDd51DwDIDhBfrYeSOUJqCQwcHwRnVYebGAmFtU1'
},
signature_method: 'HMAC-SHA1'
});
// KAW.com
// var oauth = new OAuth({
// consumer: {
// public: 'zJz6elMQsj5D',
// secret: 'KOFt1fslLCvln0mavKthZgXpvDdm0NWYgUT5oAET2ehpbV8e'
// },
// signature_method: 'HMAC-SHA1'
// })
function getRequestToken( url, data ) {
var oauthData = data;
var oauthData = null
if ( data ) {
oauthData = Object.keys( data ).reduce( ( memo, key ) => {
const value = data[ key ];
if ( Array.isArray( value ) ) {
value.forEach( ( val, index ) => memo[ `${key}[${index}]` ] = val );
} else {
memo[ key ] = value;
}
return memo;
}, {} );
}
var authorizedData = oauth.authorize( {
method: 'POST',
url: url,
data: oauthData
}, null ); // Token is still null at this point
const headers = Object.assign( oauth.toHeader( authorizedData ), {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
});
console.log( headers );
return fetch( url, {
method: 'POST',
headers: headers,
// mode: 'cors',
body: qs.stringify( data )
}).then( response => {
const contentType = response.headers.get( 'Content-Type' );
if ( contentType && contentType.indexOf( 'x-www-form-urlencoded' ) > -1 ) {
return response.text().then( text => {
return qs.parse( text )
})
}
return response.text().then( text => {
try {
var json = JSON.parse( text )
} catch( e ) {
throw { message: text, code: response.status }
}
if ( response.status >= 300) {
throw json
} else {
return json
}
})
});
}
getRequestToken( 'http://wpapi.loc/oauth1/request', {
// getRequestToken( 'http://www.kadamwhite.com/oauth1/request', {
oauth_callback: 'oob'
})
.then( result => console.log( result ) )
.catch( err => console.error( err ) );
'use strict';
var superagent = require( 'superagent' );
var qs = require( 'qs' );
var OAuth = require( 'oauth-1.0a' );
var oauth = new OAuth({
consumer: {
public: 'OL5EIwSTQyPr',
secret: 'YDBGBezQPDd51DwDIDhBfrYeSOUJqCQwcHwRnVYebGAmFtU1'
},
signature_method: 'HMAC-SHA1'
});
// KAW.com
// var oauth = new OAuth({
// consumer: {
// public: 'zJz6elMQsj5D',
// secret: 'KOFt1fslLCvln0mavKthZgXpvDdm0NWYgUT5oAET2ehpbV8e'
// },
// signature_method: 'HMAC-SHA1'
// })
function getRequestToken( url, data ) {
var oauthData = data;
var oauthData = null
if ( data ) {
oauthData = Object.keys( data ).reduce( ( memo, key ) => {
const value = data[ key ];
if ( Array.isArray( value ) ) {
value.forEach( ( val, index ) => memo[ `${key}[${index}]` ] = val );
} else {
memo[ key ] = value;
}
return memo;
}, {} );
}
var authorizedData = oauth.authorize( {
method: 'POST',
url: url,
data: oauthData
}, null ); // Token is still null at this point
const headers = Object.assign( oauth.toHeader( authorizedData ), {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
});
console.log( headers );
return new Promise(function( resolve, reject ) {
superagent.post( url )
.set( headers )
.send( qs.stringify( data ) )
.end(function( err, res ) {
if ( err ) {
return reject( err );
}
resolve( res.body );
});
});
}
getRequestToken( 'http://wpapi.loc/oauth1/request', {
// getRequestToken( 'http://www.kadamwhite.com/oauth1/request', {
oauth_callback: 'oob'
})
.then( result => console.log( result ) )
.catch( err => console.error( err ) );
var opn = require( 'opn' );
var prompt = require('prompt');
module.exports = function(config) {
opn(`http://wpapi.loc/oauth1/authorize?oauth_token=${config.token}&oauth_callback=oob`);
prompt.start();
return new Promise( ( resolve, reject ) => {
prompt.get([
'verifier'
], ( err, result ) => {
if ( err ) {
return reject( err );
}
resolve({
token: config.token,
secret: config.secret,
verifier: result.verifier
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment