Create an application in the Spotify Developer Dashboard
- First go to the Spotify Developer Dashboard.
- Log in with your Spotify account.
- Click
Create an App. - Add a name (eg. Website Now Playing) and a description.
- Click on
Show Client Secret. - Save both
Client IDandClient Secret. - Add
http://localhost:8080as redirect URI. It can also be the one where your local version of your site is running (eg.http://localhost:5000).
Get the refresh token and authenticate
You need to go to an URL first to authorize your account by logging in using your spotify account and giving permission to scopes that you will be using. These scopes let you have control on what you can share after authorizing your account. These are basically OAuth 2.0 scopes.
Here's the URL:
You need to replace the client_id with the Client ID you saved from before. The redirect_uri is the same as your local application redirect URI. It can be anything you set it.
The scope is what you give permission to access from the Spotify API. In this case, I used the user-read-currently-playing, user-read-playback-state and user-read-recently-played scopes. You can find a list of Spotify authorization scopes right here.
After authorization, it will redirect you to the redirect_uri given to the URL. That URL will have a code query parameter. You need this value.
You will use this returned code and a Base64 encoded string using the Client ID and Client Secret to retrieve the refresh token you need to access the Spotify API.
The format of the Base64 string is client_id:client_secret.
Next, let's go to a terminal and make a curl request.
This curl request will return a JSON response with our much needed refresh_token to access Spotify API. This token does not expire unless the user revokes access, so you can use this to access the API.