node-steamcommunity/components/webapi.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-02-14 13:29:21 +08:00
const SteamCommunity = require('../index.js');
2015-11-27 14:28:31 +08:00
SteamCommunity.prototype.getWebApiKey = function(domain, callback) {
this.httpRequest({
2021-07-22 16:02:38 +08:00
uri: 'https://steamcommunity.com/dev/apikey?l=english',
followRedirect: false
}, (err, response, body) => {
if (err) {
callback(err);
return;
}
2021-07-22 16:02:38 +08:00
if (body.includes('<h2>Access Denied</h2>')) {
return callback(new Error('Access Denied'));
}
2019-02-14 13:29:21 +08:00
2021-07-22 16:02:38 +08:00
if (body.includes('You must have a validated email address to create a Steam Web API key.')) {
return callback(new Error('You must have a validated email address to create a Steam Web API key.'));
}
2021-07-22 16:02:38 +08:00
let match = body.match(/<p>Key: ([0-9A-F]+)<\/p>/);
if (match) {
// We already have an API key registered
callback(null, match[1]);
} else {
// We need to register a new API key
2021-07-22 16:02:38 +08:00
this.httpRequestPost('https://steamcommunity.com/dev/registerkey?l=english', {
form: {
domain,
agreeToTerms: 'agreed',
sessionid: this.getSessionID(),
Submit: 'Register'
}
2021-07-22 16:02:38 +08:00
}, (err, response, body) => {
if (err) {
callback(err);
return;
}
2021-07-22 16:02:38 +08:00
this.getWebApiKey(domain, callback);
}, 'steamcommunity');
}
2021-07-22 16:02:38 +08:00
}, 'steamcommunity');
};