How to get the Spotify Refresh Token

In this blog, I'll show you 2 approaches to generate the Spotify Refresh Token and then use that to programmatically create an access token when needed.
I needed the Spotify Refresh Token for my blog site in which I could display my Top 10 Tracks as well as display the currently playing track in the footer section.
First Approach
Step 1: Generate your Spotify client_id and client_secret
Then select or create your app.
Note down your Client ID and Client Secret in a convenient location to use in Step 3.
Step 2: Add Redirect URIs to your Spotify app
Open settings for your app.
Add https://getyourspotifyrefreshtoken.herokuapp.com/callback to your Redirect URIs as shown in the image.

Step 3: Get your Spotify refresh Token
Add your Client ID and Client Secret to the form and select the scope for your project. More information about the scope can be found in the documentation
Click on Submit to get your refresh token.
Second Approach (Longer)
Step 1: Generate your Spotify client_id and client_secret
- Follow the steps from Approach 1 till step 2 and add <website>/callback to your Redirect URIs . Eg. http://musing.vercel.app/callback
Step 2: Create URI for access code
In the URL below, replace $CLIENT_ID , $SCOPE , and $REDIRECT_URI with the information you noted in Step 1. Make sure the $REDIRECT_URI is URL encoded.
This is how mine looked like.
Step 3: Get access code from the redirect URI
You will be redirected to your redirect URI which in my case was set to https://musing.vercel.app/callback.
In the address bar you will find a huge URL string similar to the one below. In place of $ACCESSCODE there will be a long string of characters. Note down that string for the next step.
Step 4: Get the refresh token
Type the following CURL command in your terminal and replaces all the variables with the information you noted in Step 1 and Step 3 : $CILENT_ID , $CLIENT_SECRET , $CODE , and $REDIRECT_URI .
The resulting JSON string will look something like this. Note down the refresh_token . This token will last for a very long time and can be used to generate a fresh access_token whenever it is needed.
Get Your Spotify Refresh Token With This Simple Web App
![]()
I made a simple site for developers to easily get their own refresh and access tokens for Spotify’s API. Check it out here (updated October 2022).
Motive Behind This Spotify API Authorization Wrapper
I was adding this page to my personal website that calls the Spotify API to show a brief listening history for my account.
However, to retrieve this information from the Spotify API, it requires you to log in. I didn’t want any sort of overhead for others to just see my recent songs, so I ended up setting up the authorization in this example authorization repo and going through all this trouble to just get a refresh token, which allows you to get access tokens without logging in every time. I wished there could’ve been a simple website that I could’ve easily just put in my credentials and scopes and gotten back my refresh token. So that’s what I built.
Feel free to stop reading here to go give my repo a star. This article is just to get this out there so developers looking for it might find it on Google. I figured Medium has pretty high domain authority, so this might help with that. The rest of this article is just keywords for SEO.
Spotify API Authorization Flows
- Refreshable user authorization: Authorization Code Flow
- Refreshable user authorization: Authorization Code Flow With Proof Key for Code Exchange (PKCE)
- Temporary user authorization: Implicit Grant
- Refreshable app authorization: Client Credentials Flow
Authorization code flow authorization code flow authorization code flow. Refresh token access token no login already known credentials single request. Authorization code flow authorization code flow authorization code flow. Refresh token access token no login already known credentials single request. Authorization code flow authorization code flow authorization code flow. Refresh token access token no login already known credentials single request.
Spotify API Client Credentials Scopes
Spotify API client credentials, client id, client secret, scopes. Spotify API client credentials, client id, client secret, scopes. Spotify API client credentials, client id, client secret, scopes. Ugc-image-upload user-read-recently-played user-top-read user-read-playback-position user-read-playback-state user-modify-playback-state user-read-currently-playing app-remote-control streaming playlist-modify-public playlist-modify-private playlist-read-private playlist-read-collaborative user-follow-modify user-follow-read user-library-modify user-library-read user-read-email user-read-private.
Spotify API client credentials, client id, client secret, scopes. Spotify API client credentials, client id, client secret, scopes. Spotify API client credentials, client id, client secret, scopes. Ugc-image-upload user-read-recently-played user-top-read user-read-playback-position user-read-playback-state user-modify-playback-state user-read-currently-playing app-remote-control streaming playlist-modify-public playlist-modify-private playlist-read-private playlist-read-collaborative user-follow-modify user-follow-read user-library-modify user-library-read user-read-email user-read-private.
Conclusion
Hope you enjoyed this article. And if this web app or the code in my repo helped you out in any way, please star my repo so I can get developer status points. Thank you and have a beautiful day.
Exploring the Spotify API in Python
Spotify has a very developer-friendly API one can use to stream their services via apps, websites, and other very serious ventures — or you can just tinker around with their massive music database and find out how “danceable” your 2020 playlist was.
Most tutorials on this for Python rely on the awesome spotipy package. But I prefer to bake my own janky cake before I try other people’s production-level code. And there’s a bit less out there for Python-sans-spotipy, so I thought I’d share a get-started guide here.
This blog is in three parts: first we get registered as a Spotify Developer and use our client credentials to get an access token; second we do some very basic exploration of things like album listing or track properties; third we combine all this into some more interesting analysis.
Getting access
Getting client credentials
Whether you’re using spotipy or rolling your own, first you need to get client credentials to the Spotify API.
Assuming you already have a Spotify account (free or paid), head over to Spotify for Developers and open your Dashboard. Click on “Create a Client ID” and work your way through the checkboxes.

This is designed for developing actual mobile apps and streaming services, and we are just doing some hobbyist tinkering, so there may be some confusing choices, but just pick something.
Click on the new project and you should see a “Client ID” and “Client Secret” on the left-hand side. These numbers are the only important part of this page, for us. At this point, you could use these to start working in spotipy or other pre-fab libraries out there (here’s one for R called spotifyr , of course). Or, like Stevie Nicks, you can Go Your Own Way. Read on:
Getting an access token
Now let’s open up our Python environment of choice. Right now we just need to send some GET and POST requests, so let’s use the requests library, and save those client credentials from our Spotify project’s page:
In order to access the various endpoints of the Spotify API, we need to pass an access token. There’s docs on Spotify for this, or user tutorials — but essentially we just need to POST a request with our client credentials and save the appropriate part of the response:
The access token itself is just a long alphanumeric string, like the client credentials. This is our golden ticket to access the API.
Poking around
There are a million endpoints to access things like album listings, artist information, playlists, even Spotify-generated audio analysis of individual tracks like their key, time signature, or “danceability.”
In order to access them, we send a properly formed GET request to the API server, with our access_token in the header. Let’s save this header info now, in the following very specific format:
Let’s start with checking out the audio features for a specific track, using the audio-features endpoint.
We’ll need the Track ID, which you can get by going to Spotify, clicking the “…” by a track, then “Share”, then “Copy Spotify URI”. On your clipboard is now something like: spotify:track:6y0igZArWVi6Iz0rj35c1Y . The ending is the Track ID, which is all we need:
Now we can convert this response to JSON and take a peek around:
Cool. And even cooler: the Spotify API docs give detailed explanations of the meanings of each of these values.
(Technical sidenote: earlier we did a POST request, which carried our credential information in the message body and is fairly secure. Now we’re doing a GET request, which carries the request in the URL itself, and is therefore less secure but fine for data requests like this.)
With a growing sense of the power we now wield, let’s expand our exploration.
Putting it together
Getting the data
Let’s now pick an artist, grab all their albums, all the tracks from each album, and store the audio features and analysis of each one in a big dataframe, then see what sort of interesting things we find.
(BTW, this also spares us grabbing tons of URIs by hand. No need for that, and anyway there are huge annotated lists of these, for example on Kaggle.)
I’m going to do Led Zeppelin because they have a big, varied discography which should be fun to explore (and because I’m just a huge fan). Their URI (grab it the same as for a track) is spotify:artist:36QJpDe2go2KgaRleHCDTp and the endpoint for pulling albums is artists/
Don’t forget the headers and note I’ve added a set of params to tell Spotify I only want full albums (no singles, appears-on, etc) and to give me everything (max is 50).
The jewel of this JSON is the list of albums in items , so e.g. d[‘items’][0] is a JSON of the first album in the list. We can take a look at all the albums we grabbed and their release dates:
There’s nearly 40, and lots of “duplicates”, not to mention compilations and live albums and backing tracks that we might not want to analyze. Let’s attack this by skipping duplicates as we go along, skipping everything after their last studio album in 1982, and handling individual track issues later.
We’d like to loop through each album, grab each track with albums/
Okay this is the subset of albums we were hoping for. Now we just need some way to convert a list of dicts into a nice dataframe. Pandas to the rescue:
It’s really as easy as that. We can now do some house-cleaning tasks:
Check out the first few rows:
Doing some viz
Let’s plot some of this nice data. I’m working in a Jupyter notebook, and I’m going to do a mix of seaborn and matplotlib here, so I’ll do
Since we have all these nice Spotify-generated properties for each track, a natural idea would be to look at any patterns across the albums. Let’s start with scatter plots. We can try, say, “acousticness” against “valence” (valence is basically a measure of sad (0.0) to happy (1.0)), colored by album, and sized by length of track:

Interesting to see how much these guys shifted away from the folk-inspired acoustic tracks in later albums, how a lot of their “saddest” pieces are acoustic, and how varied they are overall — the scatter covers almost the whole 1×1 plot! Niccce. Data science: validating what you kind of already knew.
This is only capturing 1 interaction though … we could keep plotting pairs of attributes … OR, we could try to find a 2-dimensional “embedding” of the entire dataset so that if two tracks are “similar” across all dimensions, they will appear “close” in the 2-dimensional scatter.
There are many approaches to this problem of “dimensionality reduction” — for example, Principal Component Analysis (PCA) is a classic method that projects points onto a lower-dimensional hyperplane in a way that maximizes the explained variance, but it’s limited in that it is strictly linear. We could instead find/learn a nonlinear surface on which to project the points, like various “manifold learning” techniques.
Let’s do this with the popular t-SNE algorithm which comes conveniently bundled with scikit-learn . t-SNE is admittedly, a bit finicky, but it sounds exotic and we are trying to have fun here people.
We can use the standard sklearn pattern of Model().fit_transform() and plot:

This is, admittedly, not as compelling as I’d hoped, but some patterns emerge: the bottom right is an eclectic mix of basically all their pre-Presence hits, from Stairway to Bron-y-Aur Stomp to Moby Dick. That little cluster of 5 songs on the middle left are all the hyper-epic ballads: In My Time of Dying, Achilles Last Stand, Carouselambra.
So in some sense, these songs share some fundamental high-dimensional similarity! Maybe! Or maybe we’re just reading the tea-leaves! Anyway, it’s all very fun.
Other ideas
Other fun things to investigate might be playlists (instead of single artists), profiling your own musical tastes like they do here, doing some more deliberate clustering of an artist’s discography, or going wild and investigating the structure of individual tracks with the audio-features endpoint that gives things temperature and pitches per beat of the song. For example, why use Spotify-meta-data like “danceability” when you could just cluster directly on the second-by-second timbre and rhythms of each song?
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Get a Spotify refresh token
rmjstn/spotify-refresh-token
Name already in use
- Local
- Codespaces
Use Git or checkout with SVN using the web URL.
Work fast with our official CLI. Learn more about the CLI.
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Get a Spotify refresh token
Spotify tokens expire after one hour. You have to use a refresh token (obtained with your client id and client secret) to get a new access token.
This application allows you to get a refresh token through the authorization code flow.
You can then use this refresh token in batch scripts for example to obtain access tokens.
Create a Spotify App
First you have to create an App by visiting the Spotify for Developers Dashboard. Note the client ID and the client Secret. On the Edit settings tab add this Redirect URIs : http://localhost:8888/callback
Configure the application
In the app.js file insert the client ID and the client Secret (lines 8 and 9).
In the app.js file insert the authorizations in the scope variable (line 29)
Start the application
Run the following commands :
Get a refresh token
Open the URL http://localhost:8888/ in a browser.
By visiting the link Login with Spotify you are prompted to sign in to Spotify. Following this, the Redirect URI (http://localhost:8888/callback) is requested.
The server console displays the tokens and user informations.
Get a new token
By visiting the link Obtain a new token using the refresh token you get a new token with a validity of one hour.