【发布时间】:2020-09-09 22:07:27
【问题描述】:
每次用户尝试使用 spotify API 搜索歌曲时,我都会生成一个访问令牌,因为我已经看到这些令牌的过期时间为 1 小时...
使用新的访问令牌,我调用了搜索端点,但得到了以下响应:
Object {
"error": Object {
"message": "No token provided",
"status": 401,
},
}
如果我将令牌传递给我的 GET 请求,我不知道为什么会这样。
代码如下:
export const searchMusic = async (query) => {
// Get an access token
const accessToken = await generateAccessToken();
console.log(accessToken); // <---- This shows the access token "BQAM...YualK"
// Request parameters
const params = {
q: query,
type: "track,album",
};
// Request url
const url = `https://api.spotify.com/v1/search${qs.stringify(params, {
addQueryPrefix: true,
})}`;
const response = await fetch(url, {
method: "GET",
header: {
Authorization: "Bearer " + accessToken, // <------- I pass the token as you can see here
},
});
const data = await response.json();
console.log(data); <----- Here comes the error
};
有人知道我做错了什么吗?另外,每次我尝试获取端点时生成一个新的令牌是不是很糟糕?我的意思是,我应该刷新它吗?
谢谢你,我将非常感谢任何指南。
【问题讨论】:
标签: javascript node.js spotify