Comment attribuer un sujet via l'API

@tobiaseigen cela fonctionne pour moi en utilisant node js

var https = require(‘https’);

// Configuration
var CONFIG = {
apiUrl: ‘YOURDISCOURSEDOMAIN’,
apiKey: ‘YOURAPIKEY’,
apiUsername: ‘YOURAPIUSER’,
assignToUsername: ‘USERNAMETOASSIGNTO’  // Nom d'utilisateur auquel attribuer les sujets
};

// IDs de sujets à attribuer
var topicIds = [634]; // changer avec l'id du sujet

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('Statut:', res.statusCode);
  console.log('Réponse brute:', data);
  try {
    var response = JSON.parse(data);
    if (res.statusCode === 200) {
      console.log('Sujet ' + topicId + ' attribué avec succès');
    } else {
      console.log('Erreur pour le sujet ' + topicId + ':', response.errors || response.error_type || response);
    }
  } catch (e) {
    console.log('Erreur d\'analyse:', e.message);
  }
  if (callback) callback();
});

});

req.on(‘error’, function(e) {
console.error('Erreur de requête pour le sujet ' + topicId + ':', e);
if (callback) callback();
});

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

// Exécuter séquentiellement
var index = 0;
function next() {
if (index < topicIds.length) {
assignTopic(topicIds[index], function() {
index++;
setTimeout(next, 500);
});
}
}

console.log('Attribution des sujets:', topicIds.join(‘, ‘));
console.log('Attribuer à:', CONFIG.assignToUsername);
console.log(’’);
next();
2 « J'aime »