This module will intercept request to WooCommerce Rest API and authenticate it using oAuth 1.0a.
Originally used in nuxt project
| import OAuth from 'oauth-1.0a'; | |
| import CryptoJS from 'crypto-js'; | |
| // Consumer Key | |
| const ck = "ck_c7796f3a7c4eaedde188c78107cd907a41944ac6"; | |
| // Consumer Secret | |
| const cs = "cs_d7a23c04e4c1939fb38fd966ea16be0dced185de"; | |
| const oauth = OAuth({ | |
| consumer: { | |
| key: ck, | |
| secret: cs, | |
| }, | |
| signature_method: 'HMAC-SHA1', | |
| hash_function: function( base_string, key ) { | |
| return CryptoJS.enc.Base64.stringify( CryptoJS.HmacSHA1(base_string, key) ); | |
| } | |
| }); | |
| function serialize(obj) { | |
| var str = []; | |
| for (var p in obj) | |
| if (obj.hasOwnProperty(p)) { | |
| str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); | |
| } | |
| return str.join("&"); | |
| } | |
| function authorize(url, method){ | |
| const requestData = { | |
| url, | |
| method | |
| } | |
| return serialize(oauth.authorize(requestData)); | |
| } | |
| export default function ({ $axios, redirect }) { | |
| $axios.interceptors.request.use(function (config) { | |
| // console.log(config); | |
| const concat_str = config.url.indexOf("?") > -1 ? "&" : "?"; | |
| const url = config.baseURL + config.url; | |
| config.url = url + concat_str + serialize( oauth.authorize({url,method:config.method})) | |
| return config; | |
| }, function (error) { | |
| // Do something with request error | |
| return Promise.reject(error); | |
| }); | |
| } |