Reputation: 133
I use to have an app that searches artist/tracks with the Spotify API. The request was pretty simple, A get request with, for example, this URL -
https://api.spotify.com/v1/search?query=bob&type=artist&market=us&limit=50&offset=0
Now it seems that I need to send also a token in the header, and for getting a token I need to connect (or register) the end user to Spotify.
Did the API flow change?
I no longer can make simple public API calls for searching tracks or artists?
Also, I see in the Spotify docs that the Authorization Required if market=from_token is supplied in the query string. Otherwise, optional.
Upvotes: 4
Views: 22900
Reputation: 1
firstly you need to get your client_id and client secret in order to access your token. using this code,
token_url = 'https://accounts.spotify.com/api/token'
message = f"{Client_ID}:{Client_secret}"
messagebase64 = base64.b64encode(message.encode())
data={
"grant_type": "client_credentials",
}
token_header={
"Authorization": f"Basic {messagebase64.decode()}"
}
r = requests.post(token_url, data=data, headers=token_header)
token = r.json()
Using this code you will get a Spotify access token in return. Then you could search a playlist or whatever using the token.
spotify_token = token['access_token']
information = requests.get(url=f"https://api.spotify.com/v1/search?q=Linkin+Park&type=track&limit=5&access_token={spotify_token}")
print(information.json())
Please check out this link Spotify API documentation
Upvotes: 0
Reputation: 707
I know its too late to answer this question. Yes, you need to get access_token and pass it with your search url to get result. Try to get Spotify access_token as follows,
func callToken() {
let parameters = ["client_id" : "your client id",// u get in developer account in Spotify.
"client_secret" : "ur secret id",
"grant_type" : "client_credentials"]
Alamofire.request("https://accounts.spotify.com/api/token", method: .post, parameters: parameters).responseJSON(completionHandler: {
response in
print(response)
print(response.result)
print(response.result.value)
if let result = response.result.value {
let jsonData = result as! NSDictionary
let token = jsonData.value(forKey: "access_token") as? String
print(token!)
}
})
}
Then save that token and call in ur search url like
search url = "https://api.spotify.com/v1/search?q=Linkin+Park&type=track&limit=5&access_token=\(token)" // pass the token in this string thats it..
Just go through online tutorial in youtube :- https://www.youtube.com/watch?v=KLsP7oThgHU&t=1s for latest version in 2019.
Download full source code with Spotify Integration + search options + default Spotify url and fetch current user's playlist and play in our native iOS App Source:- https://github.com/azeemohd786/Spotify-Demo
Upvotes: 4
Reputation: 989
All requests to the Spotify API must provide an access token. Tokens don't need to be attached to users though, you can use the Client Credentials Flow to generate an access token on behalf of your oauth client, rather than involving a user.
Can you let me know where you saw the Authorization Header marked as optional? I'd like to fix that.
Best,
Hugh
Spotify Developer Support
Upvotes: 3