繁体   English   中英

节点发布请求获取未经授权的服务器状态,但客户端获取API未获得

[英]Node post request is getting unauthorized server status but client fetch api is not

在前端发出以下提取请求时,我得到了所需的typeid值。

export const getUserProfile = () => {
  return (
    fetch(
      "https://api.spotify.com/v1/me", {
      headers: {"Authorization": "Bearer " + user_id}
    })
    .then(response => {
      return response.json()
    })
    .then(data => {
      console.log(data.type)
      console.log(data.id)
    })
  )
}

知道您无法在Node中使用fetch api之后,我使用了npm install request软件包来获取节点服务器上的数据。

request.post(authOptions, function(error, response, body) {
  var access_token = body.access_token
  let postInfo = {
    url: 'https://api.spotify.com/v1/me',
    headers: {
      "Authoriztion": "Bearer " + access_token
    },
    json: true
  }
  request.post(postInfo, function(error, response, body) {
    const route = body.type
    const current_user_id = body.id
    console.log(body)
    let uri = process.env.FRONTEND_URI || `http://localhost:3000/${route}/${current_user_id}`
    res.redirect(uri + '?access_token=' + access_token)
  })
})

这样做的目的是,当调用res.redirect时, res.redirect客户端发送到用户的主页。 但是,当客户端被重定向时,当查看为什么值未定义时,URL为http://localhost:3000/undefined/undefined?accesss_token={some token}我会在console.log(body)得到

{
  error: { 
    status: 401,
    message: 'No token provided'
  }
}

但我在记录响应时看到包含令牌

_header: 'POST /v1/me HTTP/1.1\\r\\nAuthoriztion: Bearer {some token}=\\r\\nhost: api.spotify.com\\r\\naccept: application/json\\r\\ncontent-length: 0\\r\\nConnection: close\\r\\n\\r\\n'

我可以看到为什么我的值未定义,但是为什么我在节点上却获得了未经授权的状态,而在客户端上却没有使用访存API? 另外我还注意到url access_token与服务器记录的令牌不匹配。

这是我正在使用的文档:

https://www.npmjs.com/package/request

https://developer.spotify.com/documentation/web-api/reference/users-profile/get-current-users-profile/

Github文件: https : //github.com/ryansaam/litphum-server/blob/master/server.js

如果在服务器代码中使用node-fetch ,则您将拥有与fetch类似的API。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM