Announcements

How to use API token refresh from my web app (newbie)

How to use API token refresh from my web app (newbie)

Slugger415

Hello, I'm not a pro developer but am skilled enough to be running API calls to collect ISRC codes for tracklists for a community-based online radio station. 

 

I've gotten it all working, requesting a token and running queries with it. My confusion is how to refresh the token after 60 mins. I've looked at the Token Swap and Refresh topic but it seems to have something to do with iOS-SDK (I have a Perl-based web app) and I don't understand how to do this even using Curl. My app does capture the 401 error when the token is expired but I don't know how to refresh it without telling the user to manually request a new token.

 

Any pointers and guidance appreciated. Sorry if this is a naive question.

 

thanks, Scott

 

 

Reply
1 Reply

kw0006667

There are two values you would get when you were trying to login to get your first token, access_token and refresh_token. You can either call setTimeout with 60000ms which includes a callback function to call a web api with your refresh_token.

There is an example which is written by JavaScript, but it should be similar to Curl. Or you can refresh your token by refresh_token when you got the status code is 401 (which means the access_token was expired).

 

Hope it could answer your problem

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

 

Suggested posts

Env: prod