the workaround is to implement a wrapper with https instead of request due to problem with cookies and redirections.
const SteamCommunity = require('steamcommunity');
const https = require('https');
const { URL } = require('url');
function followRedirects(url, jar, depth, callback) {
if (depth > 8) return callback('Too many redirects');
const parsed = new URL(url);
const cookieStr = Object.entries(jar).map(([k, v]) => k + '=' + v).join('; ');
const options = {
hostname: parsed.hostname,
path: parsed.pathname + parsed.search,
method: 'GET',
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Cookie': cookieStr,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36'
}
};
https.get(options, (res) => {
if (res.headers['set-cookie']) {
res.headers['set-cookie'].forEach(sc => {
const [kv] = sc.split(';');
const [k, ...v] = kv.split('=');
jar[k] = v.join('=');
});
}
let body = '';
res.on('data', d => body += d);
res.on('end', () => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
const loc = res.headers.location.startsWith('http') ? res.headers.location : parsed.origin + res.headers.location;
followRedirects(loc, jar, depth + 1, callback);
} else {
callback(null, body);
}
});
}).on('error', e => callback(e.message));
}
SteamCommunity.prototype.getTradeURL = function(callback) {
const url = 'https://steamcommunity.com/my/tradeoffers/privacy';
const cookieStr = this._jar._jar.getCookieStringSync(url);
const jar = {};
cookieStr.split('; ').forEach(pair => {
const [k, ...v] = pair.split('=');
if (k) jar[k] = v.join('=');
});
followRedirects(url, jar, 0, (err, body) => {
if (err) return callback(new Error(err));
const match = body.match(/https?:\/\/(www\.)?steamcommunity\.com\/tradeoffer\/new\/?\?partner=\d+(&|&)token=([a-zA-Z0-9_-]+)/);
if (match) callback(null, match[0], match[3]);
else callback(new Error('Malformed response'));
});
};
const community = new SteamCommunity();
// community.login({...})
community.getTradeURL((err, url, token) => {
if (err) {
console.error('getTradeURL error:', err.message);
process.exit(1);
}
console.log('Token:', token);
console.log('URL:', url);
});