gpt4 book ai didi

javascript - 使用客户端凭据流程刷新 Spotify token

转载 作者:太空宇宙 更新时间:2023-11-04 01:46:42 26 4
gpt4 key购买 nike

我已经设置了正确的客户端凭据流程,我可以获取 token 来调用电话,但在 3600 之后我想获取新的(我的应用程序仅使用“公共(public)”spotify 端点)我用https://github.com/thelinmichael/spotify-web-api-node .

抱歉我的英语不好。

const express = require('express');
const router = express.Router();
const SpotifyWebApi = require('spotify-web-api-node');

// Create the api object with the credentials
const spotifyApi = new SpotifyWebApi({
clientId: 'xxxxxxxxxxxxxxxxxxxxxxxxx',
clientSecret: 'xxxxxxxxxxxxxxxxxxxxxxxxx'
});

// Retrieve an access token.
spotifyApi.clientCredentialsGrant().then(
function(data) {
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
// console.log('The refresh token is ' + spotifyApi.getRefreshToken());
},
function(err) {
console.log('Something went wrong when retrieving an access token', err);
}
);

router.get('/getArtistAlbums', function(req, res, next) {
const user_id = req.query['id'];
spotifyApi
.getArtistAlbums(user_id, {
limit: 10,
offset: 20
})
.then(
function(data) {
res.send(data.body);
},
function(err) {
console.error(err);
}
);
});

最佳答案

无需刷新获取的 token (使用 clientCredentialsGrant 时不可能刷新),只需请求一个新 token 即可。

//This function will create a new token every time it's called
function newToken(){
spotifyApi.clientCredentialsGrant().then(
function(data) {
...

// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
},
function(err) {
... //Error management
}
);
}

//When the app starts, you might want to immediately get a new token
newToken();

//And set an interval to "refresh" it (actually creating a new one) every hour or so
tokenRefreshInterval = setInterval(newToken, 1000 * 60 * 60);

关于javascript - 使用客户端凭据流程刷新 Spotify token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51200445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com