简体   繁体   中英

How do I resolve a 'Error parsing JSON.' when trying to create a Spotify playlist with Python?

I am following the documentation trying to create a playlist with Spotify. I am getting an error message that reads: Error message It's a JSON parsing error with a status code of 400.

I was expecting a status of 200. Here is my code: My code

    # To create a playlist
playlist_endpoint = f'https://api.spotify.com/v1/users/{my_spotify_id}/playlists'

playlist_request_data = {
    'name': input('What do you want to call your playlist?').title(),
    'description': input('Describe your playlist: '),
    'public': 'false',
}

playlist_request_headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {access_code}',
}

playlist_data = requests.post(url=playlist_endpoint, data=playlist_request_data, headers=playlist_request_headers)
print(playlist_data.text)

This is the code on the Spotify API documentation: curl -X "POST" "https://api.spotify.com/v1/users//playlists" --data "{\"name\":\"New Playlist\",\"description\":\"New playlist description\",\"public\":false}" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer "

Can someone help me explain how I can make the code work?

I have resolved the issue.

I passed the 'data' argument as 'json' and it worked.

 playlist_endpoint = f'https://api.spotify.com/v1/users/{my_spotify_id}/playlists'

playlist_request_data = {
    'name': input('What do you want to call your playlist?').title(),
    'description': input('Describe your playlist: '),
    'public': 'false',
}

playlist_request_headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {access_code}',
}

playlist_data = requests.post(url=playlist_endpoint, json=playlist_request_data, headers=playlist_request_headers)
print(playlist_data.text)

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM