Coder Social home page Coder Social logo

dev-sda1 / spotify-web-presence Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 70 KB

A quick and easy way to display what your currently playing song on Spotify is on your personal website

License: GNU General Public License v3.0

JavaScript 100.00%
spotify-web-api spotify website express node-js rich-presence

spotify-web-presence's Introduction

spotify-web-presence

image image

A quick way to display what your currently playing song on Spotify is on your website or elsewhere in a JSON format. If you have any questions / improvements send me a dm or open a PR :p

ko-fi

Prerequisites

  • NodeJS and npm (sadly this was done with express)
  • Spotify Account
  • A webserver to host the endpoint on

Setting up (Server-side)

The client folder only serves as an example for how you could display it to the client.

Creating your app

1) Go to https://developer.spotify.com/dashboard, login and click "create an app" to give it a name and description.

App creation wizard

  1. You'll be given a Client ID and Client Secret. Store these somewhere as you'll need them later. Make sure to keep your Secret private. The client secret should never leave the server

Keys

  1. Click edit settings and scroll down to the Redirect URIs section. This is what spotify uses to tell our server that authentication was a success when we get to acquiring our access and refresh tokens. As this is being used on a portfolio (personal) website hopefully, all you'll need is to add localhost. Type http://localhost:1337/callback and click add, then save.

Configuring the server

1) Clone the repository into your directory of choice for the endpoint and enter the config.json file in the server folder, where you'll need to paste your client token and secret into the labelled fields. Keep the refresh token space blank that is how spotify will know it's you later.

  1. Save the config.json file and exit out, then open a terminal and run npm install && node . to install the 14 trillion node dependencies and to start it immediately.

  1. Once everything is installed, you'll be prompted in the console / terminal output to visit an auth url on Spotify's website to verify yourself. Make sure you're signed into the right spotify account before continuing.

  1. If you've entered everything on the developer console and app.js correctly, you'll be taken to an OAuth page, where you'll need to click "Accept".

  1. After clicking agree, check your console output for the tokens that spotify would've sent to you. Your access token expires after an hour, and your refresh token is used to acquire a new access token. The refresh token never expires unless you delete the app from your developer panel or you revoke access on your account settings, therefore it is absoloutely essential you keep your server secure and the refresh token secret.

  1. Copy the refresh token in it's entirety and paste it into the RefreshToken column, save the json, then stop the application. If you're doing this on a linux-based SSH session (which is likely), you'll need to exit out of the node process before you're able to edit the config. (Done by pressing CTRL+C).

  1. Restart the application, where it should now only notify you that your (access) token has been refreshed. If you get a Spotify API error complaining about an invalid refresh token, check you pasted it correctly then try again.

  1. Open spotify and play a song. You can test your endpoint by visiting http://localhost:1337/api/currentplaying where it'll show you the track's name, author(s), a direct URL, albumn art URL from their CDN, and the playing state. (Screenshot was from MS Edge Devtools. You can use this if you find the output json in the browser hard to read properly) image

You now have a spotify endpoint to show on your website!

Running the API unattended

There are many different ways that you're able to run this unattended on your production instance, I reccomend using something like PM2, although other alternatives are available.

Deploying to your server

Deploying shouldn't be too much of a hassle, and chances are you'll be running this alongside your standard Apache/NGINX install. The guides below from DigitalOcean and IONOS should help!

Closing thoughts

If you need any extra help don't be afraid to raise an issue on Github. If you want to contribute at all for some reason, you're free to open a PR too :D

API endpoint and server security is your responsibility. This program comes with ABSOLUTELY NO WARRANTY.

spotify-web-presence's People

Contributors

dev-sda1 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.