Creating Spotify Playlists with Terraform: Overcoming Spotify’s OAuth Update

Spotify recently made a change to its OAuth policy — they no longer allow http://localhost as a redirect URI for authentication flows. For developers working locally, this caused quite a disruption, especially for tools relying on OAuth like the Terraform Spotify provider. Learn more

What’s the Problem?

The original Spotify Auth Proxy server (written in Go by Conrad Ludgate) had the redirect URI as http://localhost. Since Spotify now blocks this URI for security reasons, anyone trying to authenticate would hit an INVALID_CLIENT: Invalid redirect URI error. This effectively broke the Terraform Spotify provider’s authentication flow.



The Workaround: Use 127.0.0.1 Instead of localhost

Spotify still allows redirect URIs using the IP loopback address 127.0.0.1 with HTTP, so the trick is to replace http://localhost with http://127.0.0.1. They behave the same for local testing but Spotify accepts only the latter now.

My Solution: Rebuild the Auth Proxy in JavaScript

While I could submit a pull request to the original spotify-auth-proxy repo to update the redirect URI from localhost to 127.0.0.1, I wanted a quick fix to keep things moving. So, I recreated the auth proxy server in JavaScript with the necessary changes — and it works great! I will create a PR in the original repo soon.

The main changes in my javascript server:

  • Redirect URI updated to http://127.0.0.1:27228/spotify_callback

  • Easy to run locally via Docker

  • Prints out the API Key needed for Terraform provider authentication

You can find the Docker image here: doryfazna/spotify-auth-proxy — just run it with your .env file containing your Spotify Client ID and Secret.

How to Use It with Terraform

  1. Create a Spotify Developer App with redirect URI set to http://127.0.0.1:27228/spotify_callback.

  2. Run the auth proxy server via Docker and complete the OAuth flow.

  3. Grab the API Key printed in the terminal after authentication.

  4. Pass the API Key to your Terraform provider.

  5. Write Terraform code to create playlists, search tracks, and more.


Try It Out!

Check out the GitHub repo for the code and instructions.


Why This Matters

  • Allows you to automate Spotify playlist creation using Terraform

  • Works around Spotify’s latest OAuth restrictions

  • Simple, self-hosted proxy server you can run anywhere

  • Great way to integrate Spotify into your infrastructure-as-code workflow


Comments

Popular posts from this blog

Building a Production-Grade CI/CD Pipeline with Kubernetes and Jenkins

Jenkins Master Slave Configuration. (Ubuntu instance as a slave)