HowTo: Node Red – Control Spotify via NodeRed

What I really appreciate about Spotify is the good connectivity. You can play or control music or podcasts on another device with just a few clicks.

Thanks to the free possibility of setting up developer access, many of these functions can also be accessed from NodeRed. With a few cleverly linked nodes, you can easily create your own “Spotify remote control” for (for example) the dashboard.

The control options are of course not limited to the dashboard. The control option could also be triggered, for example, via connected buttons or other options. 🙂

You can find a first approach to this in the following article.


Safety instructions

I know the following hints are always a bit annoying and seem unnecessary. But unfortunately, many people who knew it "better" from carelessness lost their eyes, fingers or other things or hurt themselves. In comparison, a loss of data is almost not worth mentioning, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Even the coolest project is worth no injury or other annoyance. https://www.nerdiy.de/en/sicherheitshinweise/

Affiliate links / advertising links

The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and shop via this link, Nerdiy.de receives a commission from the online shop or provider concerned. The price doesn't change for you. If you do your purchases via these links, you will support Nerdiy.de in being able to offer further useful projects in the future. 🙂


Requirements

Helpful Articles:
So that you can install new nodes, NodeRed should of course already be installed.
How to prepare a RaspberryPi and then install NodeRed on it is described in the following articles.

Required tools:
-none-

Required material:

In the following list you will find all the parts you need to implement this article.


Log in to the NodeRed configuration interface

Before you can edit your NodeRed configuration you must - if activated - first log in to the NodeRed configuration interface.

View of the login screen. Here you have to enter the login details that you entered during the configuration of the login. Information about this can be found in the article NodeRed - User Login Setup.

Credits

First of all, thanks to gelo2002 for his flow. The NodeCode offered below is based on its free shared code. I just added a few (useful in my opinion) extensions. The main work has been done by gelo2002. 🙂

You can find information about his flow here:
https://gist.github.com/gelo2002/1727790dfc3f6bac8c5b39f5a952b804
or here
https://flows.nodered.org/flow/1727790dfc3f6bac8c5b39f5a952b804


Install the needed nodes

So that NodeRed can communicate with your Spotify-Account you have to install the node

  • node-red-contrib-spotify

How nodes are installed is described in the article NodeRed – Installing new nodes.


Activate the Spotify Developer account

So that you can control the various functions of your Spotify account, you must first set up a developer account for your Spotify account. This is free of chage and is done with a few clicks. 🙂

To do this, go to the start page of your Spotify Developer Portal. You can use the following link:

https://developer.spotify.com/dashboard/

After logging in with your account data, you must first accept or confirm the “Conditions”.

Then click on “Create App” …

… and enter a name and a suitable description for it. You can choose both as you like, but of course they should be reasonably meaningful. 🙂

Then you will be redirected to the overview of this “app” (this is what Spotify calls the developer access). There you will find the “ClientID” and the “ClientSecret” on the left. You will need both laterso you should write them down somewhere.

After you have clicked on “Edit Settings” in the overview, you will be directed to the view shown. There you have to enter the appropriate “Redirect Uri”. This Redirect Uri depends on the address of your NodeRed server.

If this can be reached, for example, at the address https://server.fritz.box, the Redirect Uri would be as follows:

  • https://server.fritz.box:1880/spotify-credentials/auth/callback

Make sure that you select the correct protocol (i.e. http or https).

If you have entered the correct link, all you have to do is click on “Add” and (below) on “Save”.


Insert NodeCode

Now to the actual NodeCode. This includes the control of the simplest functions. So you can pause and continue playback, select the next or previous track, “scroll” in the song and get the album cover displayed.

As always, you can find the NodeCode in the following part in the Nerdiy Git repository under:

View of the NodeRed Flow.
View of the control buttons in the Dashboard.

Enter account access data in NodeRed

After you have created a developer access to your Spotify and inserted the NodeCode, you can now switch to your NodeRed configuration. Click on one of the Spotify nodes there and start configuring a new access there.

Now you have to enter your ClientID in the text field next to “ClientID” and your client secret in the text field next to “Client secret”.

You can find both in the previous step in your Spotify Developer access.

Next you have to enter the desired “Scopes”. Scopes stand for the services that you want to control from NodeRed.

For this flow here you can enter the following scopes:

streaming
user-follow-modify
user-read-currently-playing
user-modify-playback-state

You can find a complete list at: https://developer.spotify.com/documentation/general/guides/scopes/

As soon as you have entered the scopes, all you have to do is click on “Start Authentication”.

This will open a new window in which you now have to confirm that you have allowed your NodeRed to access your Spotify account.

If you get an error message at this point, you can find a solution in the paragraph “Solution for error” INVALID_CLIENT: Invalid redirect URI “. 🙂

After you have confirmed this, you will be redirected to a website on which only the short message “spotify: authorized” is displayed.


Solution for error “INVALID_CLIENT: Invalid redirect URI”

The solution to the error reporting: INVALID_CLIENT: Invalid redirect URI I found here:
https://github.com/pckhib/node-red-contrib-spotify/issues/16

In principle, the error is caused by the fact that the “Redirect URI” was not configured correctly in the developer account. To get the correct Redirect URI, you can use the following trick.

Enter your URI in the URL decoder (https://www.urldecoder.org/) and let it decode.

The marked part in the lower field then corresponds to the Redirect URI, which you have to enter in your Spotify account.


The Spotify playback jumps or stalls sometimes

Every now and then it happens that the playback on one of my devices (no matter which one) jumps or stops. By trying it out, I found out that this seems to be related to the linking of the Spotify account with external services (for example the NodeCode presented here).

In retrospect, I explain it like this: Because Spotify wants to keep the playback data synchronized on all connected participants, I can imagine that a participant who answers too slowly could bring this synchronization out of sync.

Long story short: If you also observe this error: For me it helped to restart the computer on which NodeRed is running or, in the “worst case”, to unlink the Spotify account.


I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂

Fab

P.S. Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you appreciate it that I share these information with you, I would be happy about a small donation to the coffee box. 🙂

Buy Me a Coffee at ko-fi.com

Leave a Reply

Your email address will not be published. Required fields are marked *