thelinmichael / spotify-web-api-node

A Node.js wrapper for Spotify's Web API.
http://thelinmichael.github.io/spotify-web-api-node/
MIT License
3.1k stars 499 forks source link

Problem with Refresh token #225

Open Gemiou opened 6 years ago

Gemiou commented 6 years ago

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);
            }
        );
});`
tkalejaiye commented 6 years ago

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);
    }
);
Gemiou commented 6 years ago

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);
LorenzKahl commented 6 years ago

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