簡體   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