pythonspotify

I cant add songs to my playlist at spotify


I try to add a song to my playlist at spotify. Here is my code:

import requests
import json

user_id = "user_id"
playlist_id = "playlist_id"

url = f'"https://api.spotify.com/v1/playlists/{playlist_id}/tracks"'
headers = {
    'Authorization': 'Bearer secret_auth_token',
    'Content-Type': 'application/json',
}
payload = {
    "uris": ["spotify:track:64PgpLHuQK3UJ5qZ875Vei"], 
}


response = requests.post(url, headers=headers, data=json.dumps(payload))

print(response.text)

Then I got this error: requests.exceptions.InvalidSchema: No connection adapters were found for '"https://api.spotify.com/v1/playlists/2oNkSJU1mGfTAg3ii9nLwc/tracks"'

I have tested my auth token. It has necessary scopes. For example I can create playlist with this token. But I dont know why I can't add songs to my playlist when I try it with the code above. I am new learner if u can simplify the answer that would help a lot.


Solution

  • This is due to the combination of single and double quotes you are using in your URL. Try to replace it with this and it should work as expected:

    url = f"https://api.spotify.com/v1/playlists/{playlist_id}/tracks"