Skip to content

Instantly share code, notes, and snippets.

@stefanolaru
Last active January 2, 2019 17:02
Show Gist options
  • Save stefanolaru/7644f3fa0b036e94c70125b82cf2730c to your computer and use it in GitHub Desktop.
Save stefanolaru/7644f3fa0b036e94c70125b82cf2730c to your computer and use it in GitHub Desktop.
OAuth1.0 signing request to interact with Woocommerce API / Axios /NodeJS
'use strict';
var axios = require('axios'),
oauth = require('oauth-sign'),
qs = require('querystring'),
config = require('./config.json');
// read config
var $woo = config.woo;
module.exports.woo = async (event, context) => {
var api_call = axios.get(signedApiUrl('GET', $woo.url+'/products'));
await api_call
.then(res => {
response = res.data;
})
.catch(err => {
response = err.response.data;
});
return {
statusCode: 200,
body: JSON.stringify(response)
};
}
const signedApiUrl = (method, url, params) => {
var timestamp = Date.now();
var data = {
oauth_consumer_key: $woo.consumer_key,
oauth_signature_method: 'HMAC-SHA1',
oauth_timestamp: Math.floor(timestamp/1000),
oauth_nonce: Math.random().toString(36).substring(7),
oauth_version: '1.0'
};
// add params if any
if(params) {
data = Object.assign(params, data);
}
// add signature
data = Object.assign({
oauth_signature: oauth.hmacsign(method, url, data, $woo.consumer_secret)
}, data);
return url+'?'+qs.stringify(data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment