节点发布请求正在获取未经授权的服务器状态,但客户端获取api不是
在我的前端进行以下获取请求时,我得到了我想要的type
和id
值。
export const getUserProfile = () => {
return (
fetch(
"", {
headers: {"Authorization": "Bearer " + user_id}
})
.then(response => {
return response.json()
})
.then(data => {
console.log(data.type)
console.log(data.id)
})
)
}
知道你不能在Node I中使用fetch api我使用npm install request
包来获取我的节点服务器上的数据。
request.post(authOptions, function(error, response, body) {
var access_token = body.access_token
let postInfo = {
url: '',
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
被调用时它将客户端发送到用户的主页。然而,当客户端被重定向时,网址是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\r\naccept: application/json\r\ncontent-length: 0\r\nConnection: close\r\n\r\n'
我可以看到为什么我的值未定义,但为什么我在节点中获得未经授权的状态但在客户端上没有使用fetch api?另外我注意到url access_token
与服务器记录的令牌不匹配。
以下是我正在使用的文档:
/
Github文件:.js
回答如下:如果您在服务器代码中使用node-fetch
,则您拥有与fetch
类似的API。