Spotify API 在获取端点时收到错误 "No token provided"
Spotify API getting the error "No token provided" when fetching an endpoint
每次用户尝试使用 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
};
有人知道我做错了什么吗?另外,每次我尝试获取端点时都生成一个新令牌是否不好?我的意思是,我应该刷新它吗?
谢谢,我将非常感谢任何指南。
也许您的 API 密钥无效或无法访问...
代码参考:https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401
对我来说,问题是我在客户端使用 api,而我的要求需要在服务器上 运行...所以,我将我的代码移到了服务器,一切正常!
每次用户尝试使用 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
};
有人知道我做错了什么吗?另外,每次我尝试获取端点时都生成一个新令牌是否不好?我的意思是,我应该刷新它吗?
谢谢,我将非常感谢任何指南。
也许您的 API 密钥无效或无法访问...
代码参考:https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401
对我来说,问题是我在客户端使用 api,而我的要求需要在服务器上 运行...所以,我将我的代码移到了服务器,一切正常!