Announcements

Help Wizard

Step 1

NEXT STEP

Get a permanent token for API calls

Get a permanent token for API calls

Hello,

 

I wrote a Python script to update playlists (throught API calls). It works fine, but I have to generate a new token each hour from the Spotify Web API Console. Is there a way to get a permanent token in order to be able to use it in a scheduled script ?

 

Thanks,

 

Rémy

Reply
4 Replies

Hi Remy, 

when requesting the Access Token you will also get a Refresh Token . With the Refresh Token you can generate a new Access Token if it has expired, this does not need user intervention

 

Kind Regards

 

Hi ffkangoroo,

 

I've seen this notion of "refresh token" in the documentation. But I don't know which API call to do to refresh a token received by the "Spotify Web API Console".

 

Could you give me an example of an API call ?

 

Thanks,

 

Rémy

Sure 🙂 

This is described here: Code Flow Guide 

There you will find the following Example (with the refresh_token beeing provided through the route in your app you could have stored it in the Database.): 

Example

The following example retrieves a refreshed Access Token once the current one has expired:

app.get('/refresh_token', function(req, res) {

  var refresh_token = req.query.refresh_token;
  var authOptions = {
    url: 'https://accounts.spotify.com/api/token',
    headers: { 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64')) },
    form: {
      grant_type: 'refresh_token',
      refresh_token: refresh_token
    },
    json: true
  };

  request.post(authOptions, function(error, response, body) {
    if (!error && response.statusCode === 200) {
      var access_token = body.access_token;
      res.send({
        'access_token': access_token
      });
    }
  });
});

The response will be similar to this:

{
   "access_token": "NgA6ZcYI...ixn8bUQ",
   "token_type": "Bearer",
   "scope": "user-read-private user-read-email",
   "expires_in": 3600
}

Hi,

 

A simple Node.js application to get a refresh token throught the authorization code flow. 

https://github.com/rmjstn/spotify-refresh-token

 

Rémy

Suggested posts