Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function to get Spotify access token
def accesstoken(client_id, client_secret):
header_string= base64_encode(client_id,client_secret)
headers = {
'Authorization': 'Basic '+header_string,
}
data = {
'grant_type': 'client_credentials'
}
response = requests.post('https://accounts.spotify.com/api/token', headers=headers, data=data)
access_token = json.loads(response.text)
access_token = access_token['access_token']
return(access_token)
access_token = accesstoken('your Client ID','your Client Secret')
access_token
"""
Sample response is
{'access_token': 'BQA5Zu1uNhJbKpr3tBcWRseAy-qfwwPXMjJQEvXyqKdy0Y1XaQvsC8HTE7qYuI1e_fMUVuwLltADeA-QuNc', 'token_type': 'Bearer', 'expires_in': 3600, 'scope': ''}
And using this function you don't need to worry about extracting the access_token from the JSON response.
The accesstoken function will return a string like this
'BQA5Zu1uNhJbKpr3tBcWRseAy-qfwwPXMjJQEvXyqKdy0Y1XaQvsC8HTE7qYuI1e_fMUVuwLltADeA-QuNc'
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment