'Getting a 400 Response Spotify API Python
I am trying to use requests.get with the Spotify API and I can't figure out why I keep getting a 400 response. I am working in an .ipynb file in Jupyter Lab running locally. I read these documentation but I cannot find anything relevant.
This is the response I keep getting:
{'error': {'status': 400, 'message': 'Only valid bearer authentication supported'}}
Here is my code:
# secrets.py contains important secret variables
from secrets import *
import base64
import requests
import json
# function posts to /api/token endpoint and returns access token
def spotify_get_access_token(client_id, client_secret):
url = 'https://accounts.spotify.com/api/token'
auth = base64.b64encode(f'{client_id}:{client_secret}'.encode('ascii')).decode('ascii')
headers = {'Authorization': f'Basic {auth}'}
data = {'grant_type': 'client_credentials'}
return requests.post(url, headers=headers, data=data).json()['access_token']
spotify_access_token = spotify_get_access_token(spotify_client_id, spotify_client_secret)
# function gets from specified endpoint and returns response as json
def spotify_get(endpoint, params):
url = 'https://api.spotify.com/v1' + endpoint
headers = {'Authorization': f'Bearer: {spotify_access_token}'}
return requests.get(url, headers=headers, params=params)
print(spotify_get('/artists/2xvtxDNInKDV4AvGmjw6d1/albums', {'include_groups': 'album'}).json())
This is what was printed:
{'error': {'status': 400,
'message': 'Only valid bearer authentication supported'}}
Please let me know if I am missing anything! I am working on a school project that is due soon so any help would be appreciated.
Solution 1:[1]
There shouldn't be a :
after Bearer
in the header.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Montgomery Watts |