[英]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. 在前端发出以下提取请求时,我得到了所需的
type
和id
值。
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.