Skip to content

Instantly share code, notes, and snippets.

@droganaida
Created July 24, 2018 12:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save droganaida/0d5f42f7e3a9cc041014bf6eb3adc13e to your computer and use it in GitHub Desktop.
Save droganaida/0d5f42f7e3a9cc041014bf6eb3adc13e to your computer and use it in GitHub Desktop.
List of affiliate programs admitad.com API (Basic HTTP and Bearer Token Authentication Node.js )
//==============================================================//
//********* List of affiliate programs admitad.com API *********//
// https://developers.admitad.com/
//==============================================================//
var https = require('https');
var host = 'api.admitad.com';
var port = 443;
var config = require('./config'); // your config here
var clientId = config.yourClientId; // client id
var secretKey = config.yourClientSecret; // secret key
var webId = config.yourWebSiteId; // website id
//======================= jsonGet Function =======================//
function jsonGet (url, callback){
https.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
callback(null, body);
});
}).on('error', function(e) {
callback(e);
});
}
//======================= get token =======================//
function getToken(callback) {
var scopeArray = ['advcampaigns', 'advcampaigns_for_website'];
var options = {
host: host,
port: port,
path: '/token/?grant_type=client_credentials&client_id=' + clientId + '&scope=' + scopeArray.join('%20'),
headers: {
'Authorization': 'Basic ' + Buffer.from(clientId + ':' + secretKey).toString('base64')
}
};
jsonGet(options, function(err, json){
if (err){
callback(err);
} else {
console.log('-----------token ' + json);
var responseJson = JSON.parse(json);
if (responseJson.access_token) {
callback(null, responseJson.access_token);
} else {
callback('This isnt token!')
}
}
});
}
//==============================================================//
//*************** get All Programs ******************//
//==============================================================//
function getAllPrograms(callback){
var offset = 2; // start page
var limit = 5; // programs on page
getToken(function (err, token) {
if (err) {
console.log('-----------err ' + err);
callback(err);
} else {
var options = {
host: host,
port: port,
path: '/advcampaigns/?offset=' + offset + '&limit=' + limit,
// only partners
//path: '/advcampaigns/website/' + webId + '/?connection_status=active',
headers: {
'Authorization': 'Bearer ' + token
}
};
jsonGet(options, function(err, json){
if (err){
console.log('-----------err ' + err);
callback(err);
} else {
console.log('-----------programs ' + json);
callback(null, json);
}
});
}
});
}
process.stdout.write('Get programs admitad started');
getAllPrograms(function(){
console.log('Get programs admitad finished');
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment