diff --git a/components/chat.js b/components/chat.js index c523bad..4adcef0 100644 --- a/components/chat.js +++ b/components/chat.js @@ -40,17 +40,9 @@ SteamCommunity.prototype.chatLogon = function(interval, uiMode) { this.chatState = SteamCommunity.ChatState.LoggingOn; var self = this; - this.request("https://steamcommunity.com/chat", function(err, response, body) { - if(err || response.statusCode != 200) { - self.emit('debug', 'Error requesting chat WebAPI token: ' + (err ? err.message : "HTTP error " + response.statusCode)); - self.chatState = SteamCommunity.ChatState.LogOnFailed; - setTimeout(self.chatLogon.bind(self), 5000); - return; - } - - var match = body.match(/[0-9a-f]{32}/); - if(!match) { - self.emit('debug', 'Couldn\'t find a WebAPI chat token in the response.'); + this.getWebApiOauthToken(function(err, token) { + if(err) { + self.emit('debug', "Cannot get oauth token: " + err.message); self.chatState = SteamCommunity.ChatState.LogOnFailed; setTimeout(self.chatLogon.bind(self), 5000); return; @@ -60,7 +52,7 @@ SteamCommunity.prototype.chatLogon = function(interval, uiMode) { "uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logon/v1", "form": { "ui_mode": uiMode, - "access_token": match[0] + "access_token": token }, "json": true }, function(err, response, body) { @@ -81,7 +73,7 @@ SteamCommunity.prototype.chatLogon = function(interval, uiMode) { self._chat = { "umqid": body.umqid, "message": body.message, - "accessToken": match[0], + "accessToken": token, "interval": interval }; diff --git a/components/webapi.js b/components/webapi.js new file mode 100644 index 0000000..8d19c46 --- /dev/null +++ b/components/webapi.js @@ -0,0 +1,20 @@ +var SteamCommunity = require('../index.js'); + +SteamCommunity.prototype.getWebApiOauthToken = function(callback) { + var self = this; + + // Pull an oauth token from the webchat UI + this.request("https://steamcommunity.com/chat", function(err, response, body) { + if(self._checkHttpError(err, response, callback)) { + return; + } + + var match = body.match(/"([0-9a-f]{32})"/); + if (!match) { + callback(new Error("Malformed response")); + return; + } + + callback(null, match[1]); + }); +}; diff --git a/index.js b/index.js index a2cd74a..711df82 100644 --- a/index.js +++ b/index.js @@ -145,7 +145,7 @@ SteamCommunity.prototype.getSessionID = function() { function generateSessionID() { return Math.floor(Math.random() * 1000000000); -}; +} SteamCommunity.prototype.getWebApiKey = function(domain, callback) { var self = this; @@ -333,6 +333,7 @@ require('./components/market.js'); require('./components/groups.js'); require('./components/users.js'); require('./components/inventoryhistory.js'); +require('./components/webapi.js'); require('./classes/CMarketItem.js'); require('./classes/CMarketSearchResult.js'); require('./classes/CSteamGroup.js');