Skip to content

Instantly share code, notes, and snippets.

@kaivalyapowale
Created January 19, 2020 01:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaivalyapowale/f664d0aa5b55690827bf3a4291914e3c to your computer and use it in GitHub Desktop.
Save kaivalyapowale/f664d0aa5b55690827bf3a4291914e3c to your computer and use it in GitHub Desktop.
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