Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to get a Spotify Refresh Token
Ahmed Mohamed
Ahmed Mohamed

Posted on

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 Spotifyclient_id andclient_secret

  • Go toSpotify 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 isURL encoded.
  https://accounts.spotify.com/authorize?response_type=code&client_id=$CLIENT_ID&scope=$SCOPE&redirect_uri=$REDIRECT_URI
Enter fullscreen modeExit 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 modeExit 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 tohttps://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 modeExit 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-dclient_id=$CLIENT_ID-dclient_secret=$CLIENT_SECRET-dgrant_type=authorization_code-dcode=$CODE-dredirect_uri=$REDIRECT_URI https://accounts.spotify.com/api/token
Enter fullscreen modeExit fullscreen mode
  • The resulting JSON string will look something like this. Note down therefresh_token. This token will last for a very long time and can be used to generate a freshaccess_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 modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromAhmed Mohamed

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp