Skip to content

Instantly share code, notes, and snippets.

@MuhsinFatih
Created March 8, 2018 01:54
Show Gist options
  • Save MuhsinFatih/47e0d92dfa418b7010a5f564b29b1701 to your computer and use it in GitHub Desktop.
Save MuhsinFatih/47e0d92dfa418b7010a5f564b29b1701 to your computer and use it in GitHub Desktop.
Example using the amazon PAAPI to retrieve itemsearch results with node.js
var fs = require('fs')
var colors = require('colors')
var moment = require('moment')
var sortKeys = require('sort-keys')
var cryptoJS = require('crypto-js')
var csv
var https = require('https')
var request = require('request')
var parseString = require('xml2js').parseString;
try {
csv = fs.readFileSync("PAAPICredentials.csv", 'utf8')
} catch (err) {
if(err.code !== 'ENOENT') throw err
else throw new Error("Credentials file (PAAPICredentials.csv) is missing!".yellow)
}
var lines = csv.split('\n')
var csv_cols = lines[0].split(',')
var csv_keys = lines[1].split(',')
var keys = {
associateTag: csv_keys[csv_cols.findIndex(x => x == "Associate Tag")],
accessKeyID: csv_keys[csv_cols.findIndex(x => x == "Access Key")],
secretKey: csv_keys[csv_cols.findIndex(x => x == "Secret Key")]
}
c = Object.keys(keys)
for(key in keys){
if(keys[key] === undefined) throw new Error("Credentials missing: ".yellow + key + "\nExiting!".red);
}
var endpoint = "webservices.amazon.com";
var uri = "/onca/xml";
// keys.associateTag = undefined;
var params = {
Service: "AWSECommerceService",
Operation: "ItemSearch",
AWSAccessKeyId: keys.accessKeyID,
AssociateTag: keys.associateTag,
SearchIndex: "All",
Keywords: "iphone",
ResponseGroup: "Images,ItemAttributes,Offers",
Timestamp: moment().toISOString()
};
params = sortKeys(params)
var pairs = [];
for(var param in params) {
pairs.push(encodeURIComponent(param) + "=" + encodeURIComponent(params[param]));
}
var canoncialQueryString = pairs.join("&");
var stringToSign = "GET\n" + endpoint + "\n" + uri + "\n" + canoncialQueryString;
// stringToSign = "AWSAccessKeyId=AKIAI5DAUNHSLNX52QEQ&AssociateTag=muhsinfatih-20&Keywords=iphone&Operation=ItemSearch&ResponseGroup=Images%2CItemAttributes%2COffers&SearchIndex=All&Service=AWSECommerceService&Timestamp=2018-03-07T08%3A46%3A45Z"
// console.log(stringToSign);
var signature = cryptoJS.HmacSHA256(stringToSign, keys.secretKey).toString(cryptoJS.enc.Base64);
// console.log(signature);
var requestUrl = 'https://' + endpoint + uri + '?' + canoncialQueryString + '&Signature=' + encodeURIComponent(signature);
console.log("Signed URL: \"" + requestUrl + "\"");
// lookupResult = fs.createWriteStream("lookupResult.xml");
request(requestUrl, function(error, response, body) {
// console.log('error:', error);
// console.log('statusCode', response && response.statusCode);
// console.log('body', body);
// lookupResult.write(body);
// lookupResult.end();
parseString(body, {trim: true}, function(err, result) {
console.log("%j", result.ItemSearchResponse.Items[0]);
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment