保存身份验证令牌并使用它

时间:2017-11-21 01:36:37

标签: json node.js mongodb mongodb-query gamesparks

无法确定如何从授权请求响应中保存身份验证令牌并将其用于发出授权请求,以便我可以提取数据库信息。感谢任何建议!

当我在下面的代码上运行$ node index时,我得到这个状态403响应。

$ node index
STATUS: 403
STATUS: 200
HEADERS: {"x-gs-rid":"notsureifthisinfoisconfidentialornot","content-type":"application/json;charset=utf-8","cache-control":"no-ca
che, no-store, max-age=0, must-revalidate","pragma":"no-cache","expires":"0","strict-transport-security":"max-age=31536000 ; inc
ludeSubDomains, max-age=15768000","x-xss-protection":"1; mode=block","x-frame-options":"DENY","x-content-type-options":"nosniff"
,"connection":"close","set-cookie":["mesosphere_server_id=notsureifthisprivatetoo; path=/"]}
BODY: {"country":"US","username":"myemailaddress","expiresAt":1511230469149,"X-GSAccessToken":"LotsofNumbersandLettersandPeriodsforTokenGoeshere"}

我的问题是我正在尝试保存此身份验证令牌并正确使用它,以便我可以将下一个请求(现在已经授权)发送到config2.gamesparks.net数据库,但无法弄清楚如何。

var config     = require( "./config.json" );

var gameSparks = require( "./GameSparks" );

var https = require('https');
var options = {
  host: 'auth.gamesparks.net',
  path: '/restv2/auth/user',
  headers: {
      'Authorization':'Basic PRIVATEGSAUTHCODEHERE'
  }
};

var req = https.get(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));

  // Buffer the body entirely for processing as a whole.
  var bodyChunks = [];
  res.on('data', function(chunk) {
    // You can process streamed parts here...
    bodyChunks.push(chunk);
  }).on('end', function() {
    var body = Buffer.concat(bodyChunks);
    console.log('BODY: ' + body);
    // ...and/or process the entire body here.
  })
});

req.on('error', function(e) {
  console.log('ERROR: ' + e.message);
});

var gsrequest = {
  host: 'config2.gamesparks.net',
  path: '/restv2/game/GSAPIKEYHEREcipm/endpoints/',
  }
};
var req = https.get(gsrequest, function(res) {
  console.log('STATUS: ' + res.statusCode);
});

1 个答案:

答案 0 :(得分:2)

如果你发布的是JSON,那么看起来可以通过

访问访问令牌
body.X-GSAccessToken