尝试使用访问令牌访问 spotify web api 时获取 401
Getting 401 when trying to access spotify web api with access token
我正在使用带有护照 Js 的 spotify 策略进行身份验证。然后我想使用 spotify-web-api-node 包装器库来调用 spotify 数据。但是,使用从后续调用的身份验证中获得的访问和刷新令牌是行不通的。我在尝试调用 spotify 以获取用户特定数据时收到 401 - 未经授权。
我尝试使用刷新令牌和访问令牌实例化 SpotifyWebApiObject。尽管我可以在库中看到它只需要访问令牌即可进行调用。我也尝试登录和注销以获取新的令牌集。
passport.use(new SpotifyStrategy({
clientID: keys.spotifyClientID,
clientSecret: keys.spotifyClientSecret,
callbackURL: '/auth/spotify/callback',
proxy: true
}, async (accessToken, refreshToken, profile, done) => {
const spotifyId = profile.id;
const name = profile.displayName;
const email = profile.emails[0].value;
console.log(profile.id)
const existingUser = await User.findOne({ spotifyId: profile.id });
if (existingUser) {
let userCredentials = await UserCredential.findOne({ userId: spotifyId });
if (!userCredentials) {
return await new UserCredential({ userId: spotifyId, name, accessToken, refreshToken }).save();
}
console.log('always get existing user')
userCredentials.accessToken = accessToken;
userCredentials.refreshToken = refreshToken;
return done(null, existingUser);
}
const user = await new User({ spotifyId }).save();
await new UserCredential({ userId: spotifyId, name, accessToken, refreshToken }).save();
done(null, user);
}));
一旦它们存储在数据库中。我查找用户并使用相应的访问和刷新令牌。
const getPlaylists = async (user) => {
let spotifyData = await initiateSpotifyWebApi(user);
spotifyData.getUserPlaylists(user.spotifyId).then((res) => {
console.log('response is ===', res)
}).catch((err) => console.error(err));
}
async function initiateSpotifyWebApi(user) {
const creds = await UserCredential.findOne({ userId: user.spotifyId });
const apiCaller = setUpApiObj(creds.refreshToken, creds.accessToken);
return apiCaller
}
function setUpApiObj(refreshTok, accessTok) {
const spotifyApi = new SpotifyWebApi({
accessToken: accessTok,
refreshToken: refreshTok
});
return spotifyApi;
}
getUserPlaylist()
returns 一个错误
{ [WebapiError: Unauthorized] name: 'WebapiError', message: 'Unauthorized', statusCode: 401 }
知道为什么我无法使用此库访问 api 吗?
谢谢
有几件事要检查。这就是我想到的。如果我走错了路,请纠正我,我会尽力提供进一步的帮助。
当您通过 Spotify 进行身份验证时,请检查您的 'scopes'。您需要具有正确的作用域才能对 API 执行不同的操作。看这里:https://developer.spotify.com/documentation/general/guides/authorization-guide/
如果您收到 401,请使用您的刷新令牌(我可以看到您正在存储它)自动请求、检索和覆盖您当前的 AccessToken,然后再次执行请求。
我正在使用带有护照 Js 的 spotify 策略进行身份验证。然后我想使用 spotify-web-api-node 包装器库来调用 spotify 数据。但是,使用从后续调用的身份验证中获得的访问和刷新令牌是行不通的。我在尝试调用 spotify 以获取用户特定数据时收到 401 - 未经授权。
我尝试使用刷新令牌和访问令牌实例化 SpotifyWebApiObject。尽管我可以在库中看到它只需要访问令牌即可进行调用。我也尝试登录和注销以获取新的令牌集。
passport.use(new SpotifyStrategy({
clientID: keys.spotifyClientID,
clientSecret: keys.spotifyClientSecret,
callbackURL: '/auth/spotify/callback',
proxy: true
}, async (accessToken, refreshToken, profile, done) => {
const spotifyId = profile.id;
const name = profile.displayName;
const email = profile.emails[0].value;
console.log(profile.id)
const existingUser = await User.findOne({ spotifyId: profile.id });
if (existingUser) {
let userCredentials = await UserCredential.findOne({ userId: spotifyId });
if (!userCredentials) {
return await new UserCredential({ userId: spotifyId, name, accessToken, refreshToken }).save();
}
console.log('always get existing user')
userCredentials.accessToken = accessToken;
userCredentials.refreshToken = refreshToken;
return done(null, existingUser);
}
const user = await new User({ spotifyId }).save();
await new UserCredential({ userId: spotifyId, name, accessToken, refreshToken }).save();
done(null, user);
}));
一旦它们存储在数据库中。我查找用户并使用相应的访问和刷新令牌。
const getPlaylists = async (user) => {
let spotifyData = await initiateSpotifyWebApi(user);
spotifyData.getUserPlaylists(user.spotifyId).then((res) => {
console.log('response is ===', res)
}).catch((err) => console.error(err));
}
async function initiateSpotifyWebApi(user) {
const creds = await UserCredential.findOne({ userId: user.spotifyId });
const apiCaller = setUpApiObj(creds.refreshToken, creds.accessToken);
return apiCaller
}
function setUpApiObj(refreshTok, accessTok) {
const spotifyApi = new SpotifyWebApi({
accessToken: accessTok,
refreshToken: refreshTok
});
return spotifyApi;
}
getUserPlaylist()
returns 一个错误
{ [WebapiError: Unauthorized] name: 'WebapiError', message: 'Unauthorized', statusCode: 401 }
知道为什么我无法使用此库访问 api 吗?
谢谢
有几件事要检查。这就是我想到的。如果我走错了路,请纠正我,我会尽力提供进一步的帮助。
当您通过 Spotify 进行身份验证时,请检查您的 'scopes'。您需要具有正确的作用域才能对 API 执行不同的操作。看这里:https://developer.spotify.com/documentation/general/guides/authorization-guide/
如果您收到 401,请使用您的刷新令牌(我可以看到您正在存储它)自动请求、检索和覆盖您当前的 AccessToken,然后再次执行请求。