Come assegno un argomento tramite API

@tobiaseigen questo funziona per me usando node js

var https = require(‘https’);

// Configurazione
var CONFIG = {
apiUrl: ‘YOURDISCOURSEDOMAIN’,
apiKey: ‘YOURAPIKEY’,
apiUsername: ‘YOURAPIUSER’,
assignToUsername: ‘USERNAMETOASSIGNTO’  // Nome utente a cui assegnare gli argomenti
};

// ID degli argomenti da assegnare
var topicIds = [634]; // cambia con l'ID dell'argomento

function assignTopic(topicId, callback) {
var postData = JSON.stringify({
target_id: topicId,
target_type: ‘Topic’,
username: CONFIG.assignToUsername
});

var options = {
hostname: CONFIG.apiUrl,
port: 443,
path: ‘/assign/assign.json’,
method: ‘PUT’,
rejectUnauthorized: false,
headers: {
‘Api-Key’: CONFIG.apiKey,
‘Api-Username’: CONFIG.apiUsername,
‘Content-Type’: ‘application/json’,
‘Content-Length’: postData.length
}
};

var req = https.request(options, function(res) {
var data = ‘’;

res.on('data', function(chunk) {
  data += chunk;
});

res.on('end', function() {
  console.log('Stato:', res.statusCode);
  console.log('Risposta grezza:', data);
  try {
    var response = JSON.parse(data);
    if (res.statusCode === 200) {
      console.log('Argomento ' + topicId + ' assegnato con successo');
    } else {
      console.log('Errore argomento ' + topicId + ':', response.errors || response.error_type || response);
    }
  } catch (e) {
    console.log('Errore di analisi:', e.message);
  }
  if (callback) callback();
});

});

req.on(‘error’, function(e) {
console.error('Errore richiesta per argomento ’ + topicId + ‘:’, e);
if (callback) callback();
});

req.write(postData);
req.end();
}

// Esegui in sequenza
var index = 0;
function next() {
if (index < topicIds.length) {
assignTopic(topicIds[index], function() {
index++;
setTimeout(next, 500);
});
}
}

console.log('Assegnazione argomenti:', topicIds.join(‘, ‘));
console.log('Assegna a:', CONFIG.assignToUsername);
console.log(’’);
next();
2 Mi Piace