spotify-web-api-node icon indicating copy to clipboard operation
spotify-web-api-node copied to clipboard

Problem with Refresh token

Open Gemiou opened this issue 8 years ago • 3 comments

I have setup correct my Client Credential flow and i can get my token to make my calls but after 3600 i want to get new (My app use only "public" spotify endpoints) Sorry for my english.

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);
            }
        );
});`

Gemiou avatar Jul 05 '18 21:07 Gemiou

Sorry if I'm misunderstanding your question, but the following code should let you reset and save your access token so you can continue making calls:

// clientId, clientSecret and refreshToken has been set on the api object previous to this call.
spotifyApi.refreshAccessToken().then(
	function(data) {
		console.log("The access token has been refreshed!");

		// Save the access token so that it's used in future calls
		spotifyApi.setAccessToken(data.body["access_token"]);
	},
	function(err) {
		console.log("Could not refresh access token", err);
	}
);

tkalejaiye avatar Jul 16 '18 18:07 tkalejaiye

Hello to retrieve an access token ,I call the function getToken after 3600.

getoken();
// Retrieve an access token.
function getoken() {
// 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']);
            spotifyApi.setAccessToken(data.body['access_token']);
        },
        function (err) {
            console.log('Something went wrong when retrieving an access token', err);
        }
    );
}
setInterval(getoken, 1000 * 60 * 60);

Gemiou avatar Jul 16 '18 19:07 Gemiou

Hi, instead of clientCredentialsGrant you might want to consider using authorizationCodeGrant - the client credentials authorization flow does not support refreshing of the access token - see: https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-flows

LorenzKahl avatar Sep 04 '18 21:09 LorenzKahl