首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >spotify-web节点: authorizationCodeGrant()给出一个400 -坏的请求

spotify-web节点: authorizationCodeGrant()给出一个400 -坏的请求
EN

Stack Overflow用户
提问于 2017-12-26 21:39:57
回答 1查看 1.5K关注 0票数 1

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

我按照给定的这里示例从Spotify获得授权代码。然后,我使用这个代码从Spotify获取访问令牌刷新令牌,并执行我想要的所有操作。

当我在这里询问访问令牌时,就会出现问题:

代码语言:javascript
运行
复制
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 !' })
    })
})

此代码记录:

代码语言:javascript
运行
复制
Something went wrong! { [WebapiError: Bad Request] name: 'WebapiError', message: 'Bad Request', statusCode: 400 }

我的密码怎么了?如何从Spotify获得访问令牌和刷新令牌?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-27 21:21:53

问题很简单(我在上面浪费了两天.)。正如Spotify文档这里中所指定的那样。在谈到redirect_uri参数时,文档说:

TLDR读到:

此参数的值必须与请求授权代码时提供的redirect_uri值完全匹配。

所以在我的代码中:

代码语言:javascript
运行
复制
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 !
    })

    // [...]

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47983963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档