I am having a ridiculous time trying to get authenticated to use the Spotify API with oauth. Here is my code:
import os
import spotipy
import spotipy.util as util
from json.decoder import JSONDecodeError
# Gives Spotify credentials:
username = "MY_USERNAME"
scope = "user-read-currently-playing"
# Gives developer app credential:
ID = "MY_ID_FROM_CREATED_SPOTIFY_APP"
SECRET = "MY_SECRET_FROM_CREATED_SPOTIFY_APP"
URI = "http://google.com/"
# Erase cache and prompt for user permission:
try:
token = util.prompt_for_user_token(username, scope, ID, SECRET, URI)
except (AttributeError, JSONDecodeError):
os.remove(".cache-" + username)
token = util.prompt_for_user_token(username, scope, ID, SECRET, URI)
# Creates Spotify Object
sp = spotipy.Spotify(auth=token)
I have watched multiple tutorials, read through countless other questions, and tried on multiple computers and code editors (I'm using PyCharm), and everytime I get spotipy.oauth2.SpotifyOauthError: Bad Request
.
I really don't know what else to try here, and what is crazy to me is that I used this same code (with a different app ID) for a different project a few months ago. I was able to successfully get authenticated following the same steps. I'm clearly making one key mistake everywhere I try this though. I've restarted three times. Some things I've tried: using a different redirect URI (I tried localhost and all the other recommendations), making sure I whitelisted the URI, making sure I have the backslash at the end of the redirect URI. The only recommendation I haven't tried is "deleting the .spotify-token-cache file from my configure folder". I'm on PyCharm on Windows, and can't for the life of me find this ".spotify-token-cache". Any other possibilities? I just don't know why this wouldn't work now, after it did in the past. Sorry if this is long-winded, I'm just at my wits' end.