Created
January 19, 2020 01:16
-
-
Save kaivalyapowale/f664d0aa5b55690827bf3a4291914e3c to your computer and use it in GitHub Desktop.
Function to get Spotify access token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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