2017-12-26 8 views
1

쓸만한 코드를 작성하지 않고도 Spotify 웹 API를 사용하려면 npm 모듈 spotify-web-api-node을 사용합니다.spotify-web-api-node : authorizationCodeGrant()가 400 개의 잘못된 요청을 내림

나는 here 주어진 예제를 따라 인증 코드 Spotify에서. 그런 다음이 코드을 사용하여 액세스 토큰새로 고침 토큰을 Spotify에서 가져 와서 원하는 모든 작업을 수행합니다.

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에서 액세스 토큰과 새로 고침 토큰을 얻으려면 어떻게해야합니까? 고맙습니다 !

+0

Spotify API에 대해 알지 못해도 redirectUri는 의심 스럽습니다. 공개 URL이 아니어야합니까? –

+0

글쎄, localhost가 인증 코드를 요청할 때 완벽하게 작동합니다. –

+0

공개 도메인에서 시도했지만 작동하지 않습니다. –

답변

2

문제는 간단했습니다 (나는 2 일 낭비했습니다 ...). Spotify API 설명서 here에 지정된대로.

TLDR READ THIS : 설명서를 말한다 redirect_uri 매개 변수에 대해 말하기

이 매개 변수의 값은 정확하게 인증 코드를 요청할 때 제공 redirect_uri로의 값과 일치해야합니다. 그래서 내 코드에서

:

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 ! 
    }) 

    // [...] 

내가 당신에게 말 전체 코드를 제공하지 않았기 때문에 아무도 문제를 발견 한 수 있기 때문에 나는 또한 사과 "첫 번째 부분의 일을!". 예, 작동하지만 내 문제에 대한 유용한 단서가 포함되어 있습니다.

관련 문제