简体   繁体   English

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

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

When making the following fetch request on my front-end I'm getting my desired type and id values. 在前端发出以下提取请求时,我得到了所需的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)
    })
  )
}

Knowing you can't use fetch api in Node I used the npm install request package to get the data on my node server. 知道您无法在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)
  })
})

The purpose of doing this is so when the res.redirect gets called it sends the client to the user's home page. 这样做的目的是,当调用res.redirect时, res.redirect客户端发送到用户的主页。 However when the client gets redirected the url is http://localhost:3000/undefined/undefined?accesss_token={some token} when looking why the values are undefined I console.log(body) and I get 但是,当客户端被重定向时,当查看为什么值未定义时,URL为http://localhost:3000/undefined/undefined?accesss_token={some token}我会在console.log(body)得到

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

but I can see when logging the response that the token is included 但我在记录响应时看到包含令牌

_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'

I can see why my values are undefined but why am I getting an unauthorized status in node but not on the client using fetch api? 我可以看到为什么我的值未定义,但是为什么我在节点上却获得了未经授权的状态,而在客户端上却没有使用访存API? Also I noticed that the url access_token doesn't match the server logged token. 另外我还注意到url access_token与服务器记录的令牌不匹配。

Here are the docs I'm using: 这是我正在使用的文档:

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

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

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

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

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

相关问题 如何通过本地客户端和服务器的fetch API发出POST请求? - How to make POST request via fetch API with local client and server? 无法从前端客户端向本地节点服务器发出 fetch post 请求,只有 GET 请求有效 - Cant make a fetch post request from front end client to local node server, only GET requests work 编辑:无法使用 Fetch API 向 Express 服务器发送 POST 请求,不断收到 JSON.parse 错误 - EDITED: Can't send POST request to Express server with Fetch API, keep getting JSON.parse error 获取针对Web请求的HTTP状态401未经授权? - Getting http status 401 unauthorized for the web request? Fetch API请求收到401未授权访问 - Fetch API request receives 401 unauthorized access 使用 Fetch API 发布请求? - POST Request with Fetch API? 在将api请求发送到服务器时将api更改POST请求更改为GET请求 - fetch api change POST request to GET request when send it to the server HTML 到 Node.js 服务器 POST 请求在 localhost 上使用 Fetch - HTML to Node.js server POST request on localhost with Fetch 获取 http POST 请求发送 EMPTY object 到节点服务器 - Fetch http POST request sending EMPTY object to Node server 从React Client获取post api以表示服务器导致错误 - Fetch post api from React Client to express server causes error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM