@tobiaseigen 这在我的 node js 上运行正常
var https = require(‘https’);
// 配置
var CONFIG = {
apiUrl: ‘YOURDISCOURSEDOMAIN’,
apiKey: ‘YOURAPIKEY’,
apiUsername: ‘YOURAPIUSER’,
assignToUsername: ‘USERNAMETOASSIGNTO’ // 要分配话题的用户名
};
// 要分配的话题 ID
var topicIds = [634]; // 用话题 ID 更改
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('状态:', res.statusCode);
console.log('原始响应:', data);
try {
var response = JSON.parse(data);
if (res.statusCode === 200) {
console.log('话题 ' + topicId + ' 分配成功');
} else {
console.log('话题 ' + topicId + ' 错误:', response.errors || response.error_type || response);
}
} catch (e) {
console.log('解析错误:', e.message);
}
if (callback) callback();
});
});
req.on(‘error’, function(e) {
console.error('话题 ' + topicId + ' 的请求错误:', e);
if (callback) callback();
});
req.write(postData);
req.end();
}
// 顺序运行
var index = 0;
function next() {
if (index < topicIds.length) {
assignTopic(topicIds[index], function() {
index++;
setTimeout(next, 500);
});
}
}
console.log('正在分配话题:', topicIds.join(‘, ‘));
console.log('分配给:', CONFIG.assignToUsername);
console.log(’’);
next();