Ich verwende das Npm-Modul spotify-web-api-node
, um die Spotify Web API zu verwenden, ohne eine Tonne Code zu schreiben.Spotify-Web-API-Knoten: AuthorizationCodeGrant() Geben Sie eine 400 - Bad Request
Ich folgte dem Beispiel gegeben , um einen Autorisierungscode von Spotify zu bekommen. Dann verwende ich diesen Code, um einen Access Token und einen Refresh Token von Spotify zu erhalten und alle Aktionen auszuführen, die ich möchte.
Das Problem tritt auf, wenn ich fragen die Zugriffstoken hier:
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 !' })
})
})
Dieser Code-Protokolle:
Something went wrong! { [WebapiError: Bad Request] name: 'WebapiError', message: 'Bad Request', statusCode: 400 }
Was mit meinem Code falsch? Was kann ich tun, um den Access Token und das Refresh Token von Spotify zu erhalten? Vielen Dank !
Ich weiß nichts über Spotify API, aber Ihre RedirectUri scheint verdächtig zu sein. Sollte es nicht eine öffentliche URL sein? –
Nun, Localhost funktioniert perfekt, wenn ich es benutze fragt nach dem Authorization Code –
Ich versuchte mit einer Public Domain, funktioniert nicht –