Javascript 节点JS尝试将访问令牌分配给变量

Javascript 节点JS尝试将访问令牌分配给变量,javascript,html,node.js,spotify,Javascript,Html,Node.js,Spotify,我是node.js的新手,正在尝试进行spotify API请求,但不幸的是,我的access token变量出错 我使用的是以下代码,基本上是从他们的官方示例程序中逐字提取的: var access_token; app.get('/callback', function(req, res) { // your application requests refresh and access tokens // after checking the state parameter var

我是node.js的新手,正在尝试进行spotify API请求,但不幸的是,我的access token变量出错

我使用的是以下代码,基本上是从他们的官方示例程序中逐字提取的:

var access_token;


app.get('/callback', function(req, res) {

// your application requests refresh and access tokens
// after checking the state parameter

var code = req.query.code || null;
var state = req.query.state || null;
var storedState = req.cookies ? req.cookies[stateKey] : null;

if (state === null || state !== storedState) {
res.redirect('/#' +
  querystring.stringify({
    error: 'state_mismatch'
  }));
} else {
res.clearCookie(stateKey);
var authOptions = {
  url: 'https://accounts.spotify.com/api/token',
  form: {
    code: code,
    redirect_uri: redirect_uri,
    grant_type: 'authorization_code'
  },
  headers: {
    'Authorization': 'Basic ' + (new Buffer(client_id + ':' +      client_secret).toString('base64'))
  },
  json: true
};

request.post(authOptions, function(error, response, body) {
  if (!error && response.statusCode === 200) {

    access_token = body.access_token,
        refresh_token = body.refresh_token;

    var options = {
      url: 'https://api.spotify.com/v1/me',
      headers: { 'Authorization': 'Bearer ' + access_token },
      json: true
    };

    // use the access token to access the Spotify Web API
    request.get(options, function(error, response, body) {
      console.log(body);
    });

    // we can also pass the token to the browser to make requests from there
    res.redirect('/#' +
      querystring.stringify({
        access_token: access_token,
        refresh_token: refresh_token
      }));
  } else {
    res.redirect('/#' +
      querystring.stringify({
        error: 'invalid_token'
      }));
  }
});
}

所以我有var access_token,然后我尝试将access_token设置为body.access_token,我假设它应该是access token的值。但是,当我运行console.log(“我的访问令牌:+access\u令牌”)时,它会说我的访问令牌未定义!这对我来说如此混乱的原因是,对应的HTML文件(它似乎在其中发送访问令牌)显示得非常好


有人知道我可能做错了什么吗?我觉得我现在还没有很好地把握全局。

响应正文可能是纯文本,需要解析。试着做

body = JSON.parse(body);

在设置
access\u-token
refresh\u-token

之前,是否可以将
主体
变量的内容粘贴到此处?