Logo

dev-resources.site

for different kinds of informations.

How to get a Spotify Refresh Token

Published at
1/14/2024
Categories
webdev
api
spotify
json
Author
moezarella
Categories
4 categories in total
webdev
open
api
open
spotify
open
json
open
Author
10 person written this
moezarella
open
How to get a Spotify Refresh Token

In this blog, I'll show you how to generate the Spotify Refresh Token and then use that to programmatically create an access token when needed.

I needed the Spotify Refresh Token to display the currently playing track in the footer section.

The Approach


Step 1: Generate your Spotify client_id and client_secret

  • Go to Spotify developers dashboard.

  • 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: 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.
  https://accounts.spotify.com/authorize?response_type=code&client_id=$CLIENT_ID&scope=$SCOPE&redirect_uri=$REDIRECT_URI
Enter fullscreen mode Exit fullscreen mode
  • This is how mine looked like.
  https://accounts.spotify.com/authorize?response_type=code&client_id=CLIENT_ID&scope=SCOPE&redirect_uri=https%3A%2F%2Fahmedrelated.com%2Fcallback
Enter fullscreen mode Exit fullscreen mode

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://ahmedrelated.com/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.

  https://ahmedrelated.com/callback?code=$ACCESSCODE
Enter fullscreen mode Exit fullscreen mode

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.
  curl -d client_id=$CLIENT_ID -d client_secret=$CLIENT_SECRET -d grant_type=authorization_code -d code=$CODE -d redirect_uri=$REDIRECT_URI https://accounts.spotify.com/api/token
Enter fullscreen mode Exit fullscreen mode
  • 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.
  {
    "access_token": "ACCESS_TOKEN",
    "token_type": "Bearer",
    "expires_in": 3600,
    "refresh_token": "REFRESH_TOKEN",
    "scope": "playlist-modify-private"
  }
Enter fullscreen mode Exit fullscreen mode
spotify Article's
30 articles in total
Favicon
Essential Features of a Music Streaming App Like Spotify
Favicon
Troubleshooting Spotify Proxy Errors
Favicon
How to Solve Spotify Proxy Problems
Favicon
This is all about Instafest
Favicon
How spoken.host Simplifies Spotify API Authentication
Favicon
Automating Spotify Playlist Creation with Terraform: My Journey to "Perfect"
Favicon
What Is a Proxy Service for Spotify and How to Set It Up
Favicon
Advice from Software Engineers at Google and Spotify
Favicon
UI/Text Blur Issues with Electron-Based Applications (e.g., Spotify, Discord) on Wayland
Favicon
OAuth Tutorial with Go and the Spotify API
Favicon
Create Custom Spotify Playlist Covers: A Developer's Journey
Favicon
Design Spotify
Favicon
Spotify Premium APK v8.10.9.722 Descargar Ultima Versi贸n 2024
Favicon
馃殌 Introducing SpotAPI: A Python Library for Effortless Spotify Integration 馃殌
Favicon
Spotify Premium APK Descargar 煤ltima versi贸n para Android 2024
Favicon
Playlists created and managed seamlessly with Terraform!
Favicon
Spotify Premium APK Descargar 脷ltima versi贸n para Android 2024
Favicon
Adding Spotify Now Playing to Your React App
Favicon
Unlock the Full Potential of Spotify with Spotify Premium APK
Favicon
CREATE SPOTIFY PLAYLIST USING TERRAFORM
Favicon
Bundling Code-Bases with Large File Structures
Favicon
Clonador y Fusionador de Playlists para Spotify
Favicon
Spotify MOD APK v8.9.6.458: Download (No Ads/Premium) 2024
Favicon
Hide Rick Rolls in a Spotify Link
Favicon
GO Spotify CLI
Favicon
I made a web app that creates Spotify playlists for you
Favicon
驴Escuchar m煤sica en Spotify tiene anuncios?
Favicon
How to get a Spotify Refresh Token
Favicon
A Brief Look At Spotify's Tech Stack
Favicon
How To Create Spotify Stats Charts With Spotify Pie?

Featured ones: