Same. Am able to get authorized but actually getting a search query to work is a different story.
Thanks @HarshalSeth @JustinUplinger @Raschoe for your comments.
We are revisiting the 100 Days of Code tasks at the moment to check + tidy up. Iâll make sure to update Day 93 with the most recent information as soon as possible.
All good, thanks so much! Yeah Spotify just changed their documentation so itâs a little confusing. Will try to figure it out in the meantime and post if I can figure it out
Got it to work. Found thatâŚ
The lesson was missing the line data = response.json()
before trying to print out the response data. It is in the solution though.
Some info on how the queries are formatted would be helpful, like explaining that â%20â means a space in a URL etc. so itâs easier to understand the example query given.
This worked for me, with {inputYear}
being the variable for year pulled from the form:
search = f"?q=year:{inputYear}&type=track&market=US&limit=10&offset=5&include_external=audio"
I don´t understand it, where can I put in the access token in spotify?
Can anyone tell me why this code doesnât work?
https://replit.com/@Sai-GiridharGi1/Day-93100Days#main.py
when you open it in a new tab and add /boom to the end. It says method not allowed
Cause you only set the method to POST
@app.route("/boom", methods=["POST"])
change that to methods=["GET"]
(or you can just remove the methods
kwarg entirely cause the method defaults to GET
)
Thank you so much for trying to help I really appreciate it because this has been keeping me up at night, I really thank you. But when I remove the methods kwarg and I go to the site it says âbad requestâ
Oh sorry, didnât realize, but basically, you run the app before creating the /
route. So you need to move the app.run
to after you define the /
route.
it still says âbad requestâ
Okay hopefully this is the last error lol
Basically, youâre not returning the page after defining the variable. No idea how I missed this earlier.
Bruhs,
I get my âaccess_tokenâ correctly (Iâve checked it in the previous step by step), but when I copy and paste this code of David and I enter any band as a query it says:
Artist: Nirvana
{
âgrant_typeâ: âclient_credentialsâ
}
Traceback (most recent call last):
File âmain.pyâ, line 26, in
for track in data[âtracksâ][âitemsâ]:
KeyError: âtracksâ
Iâm lost. Please S.O.S.
p.s. the code of David here:
import requests, json, os # Missing import
from requests.auth import HTTPBasicAuth
clientID = os.environ['CLIENT_ID']
clientSecret = os.environ['CLIENT_SECRET']
url = "https://accounts.spotify.com/api/token"
data = {"grant_type":"client_credentials"}
auth = HTTPBasicAuth(clientID, clientSecret)
response = requests.post(url, data=data, auth=auth)
accessToken = response.json()["access_token"]
artist = input("Artist: ").lower() # Variable identifier typo
artist = artist.replace(" ", "%20")
url = "https://api.spotify.com/v1/search"
headers = {'Authorization': f'Bearer {accessToken}'}
search = f"?q=artist%3A{artist}&type=track&limit=5" # Missing 'q' in the URL - yes, this will break the code! Any incorrect URL will
fullURL = f"{url}{search}"
response = requests.get(fullURL, headers=headers)
print(json.dumps(data, indent=2))
for track in data["tracks"]["items"]:
print(track["name"])
print(track["preview_url"])
*** UPDATE / SOLUTION **
as @JustinUplinger said, data = response.json()
is missing right after the line 23 of the code I have just pasted above.
@DavidAtReplit , @QwertyQwerty88 , bruhs, that is missing! It was giving me hell! Fix it before someone elseâs head collapses!
So I was going to post a question here but I managed to solve my problem before posting so Iâd like to help people working through this dayâs challenge with what I discovered. As other people have mentioned, it seems like Spotify changed the way in which you use the OAuth Token Auth section, but we donât need to do what David does in the video where he plugs in the token to the website to experiment with the search query (I still donât know how to do that, honestly). If you know how the syntax of the query works in the URL, that will be enough. The code below has a single error and fixing it wouldâve prevented me from going down the rabbit hole of trying to figure out extra non-existent authorization steps to pull a successful query. Anyways, there needs to be a space between Bearer
and {accessToken}
.
Code
import json
import requests, os
from requests.auth import HTTPBasicAuth ## attaches basic authentication to the given request object
clientId = os.environ['CLIENT_ID'] ## "username"
clientSecret = os.environ['CLIENT_SECRET'] ## "password"
url = "https://accounts.spotify.com/api/token" ## where to get the token
data = {"grant_type": "client_credentials"} ## what to get
auth = HTTPBasicAuth(clientId, clientSecret) ## authentifies my permission to send the request
response = requests.post(url, data = data, auth = auth) ## stores the response
## print(response.ok) ## check if the call is working
## print(response.json())
## print(response.status_code)
accessToken = response.json()["access_token"] ## grab the token
url = "https://api.spotify.com/v1/search"
headers = {"Authorization": f"Bearer{accessToken}"}
search = "?q=artist%3Aknowmads&type=track&limit=10"
fullURL = f"{url}{search}"
response = requests.get(fullURL, headers=headers)
data = response.json()
print(json.dumps(data, indent=2))
Can anyone please link a repl with Davidâs full solution? His repl linked on the video doesnât work.