gpt4 book ai didi

node.js - spotify-web-api- Node : authorizationCodeGrant() give a 400 - Bad Request

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

我使用 npm 模块 spotify-web-api-node 来使用 Spotify Web API,而无需编写大量代码。

我按照给出的例子 here从 Spotify 获取授权代码。然后,我使用此代码从 Spotify 获取访问 token 刷新 token ,并执行我想要的所有操作。

当我在此处询问访问 token 时,出现问题:

router.get('/auth/spotify/success', (req, res, next) => {
let spotifyApi = new SpotifyWebApi({
clientId: 'my-client-id',
clientSecret: 'my-client-secret',
redirectUri: 'http://localhost:3000/'
// The URI is registered to Spotify redirect URIs
})

const code = req.query.code

spotifyApi.authorizationCodeGrant(code)
.then(data => {
console.log('The token expires in ' + data.body['expires_in'])
console.log('The access token is ' + data.body['access_token'])
console.log('The refresh token is ' + data.body['refresh_token'])

// Set the access token on the API object to use it in later calls
spotifyApi.setAccessToken(data.body['access_token'])
spotifyApi.setRefreshToken(data.body['refresh_token'])


res.render('index', { title: 'Connected !' })
})
.catch(err => {
console.log('Something went wrong!', err);

res.render('index', { title: 'Error !' })
})
})

此代码记录:

Something went wrong! { [WebapiError: Bad Request] name: 'WebapiError', message: 'Bad Request', statusCode: 400 }

我的代码有什么问题吗?我该如何获得 Spotify 的访问 token 和刷新 token ?谢谢!

最佳答案

问题很简单(我浪费了两天时间......)。如 Spotify API 文档 here 中所述。谈到 redirect_uri 参数,文档说:

TLDR 阅读此内容:

The value of this parameter must exactly match the value of redirect_uri supplied when requesting the authorization code.

所以在我的代码中:

router.get('/auth/spotify/success', (req, res, next) => {
let spotifyApi = new SpotifyWebApi({
clientId: 'my-client-id',
clientSecret: 'my-client-secret',
redirectUri: 'http://localhost:3000/'
// Changing this...
redirectUri: 'http://localhost:3000/auth/spotify/success'
// ...to this made it work !
})

// [...]

我也很抱歉,因为没有人能发现这个问题,因为我没有给你完整的代码说“第一部分有效!”。是的,它有效,但它包含有关我的问题的有用线索。

关于node.js - spotify-web-api- Node : authorizationCodeGrant() give a 400 - Bad Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47983963/

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