Skip to content

Instantly share code, notes, and snippets.

@gamer191
Last active April 23, 2026 06:40
Show Gist options
  • Select an option

  • Save gamer191/9136eecd38927b752e659a3d7d8fa8a2 to your computer and use it in GitHub Desktop.

Select an option

Save gamer191/9136eecd38927b752e659a3d7d8fa8a2 to your computer and use it in GitHub Desktop.
Spotify Authentication token generator

This program uses code from https://github.com/RomeoDespres/pkce:

MIT License

Copyright (c) 2020 Roméo Després

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Also uses code from https://stackoverflow.com/a/5075477 and https://developer.spotify.com/documentation/web-api/tutorials/code-pkce-flow, as well as exmaple code from https://docs.python.org/

# See LICENSE file for licensing information
import argparse
import base64
import hashlib
import http.cookiejar
import json
import secrets
import urllib.parse
import urllib.request
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--cookies')
args = parser.parse_args()
code_verifier = ''.join(secrets.SystemRandom().choices('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=128))
code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode()).digest()).decode()[:-1]
# values derived from https://developer.spotify.com/
client_id = 'cfe923b2d660439caf2b557b21f31221'
scope = 'email openid profile user-self-provisioning playlist-modify-private playlist-modify-public playlist-read-collaborative playlist-read-private ugc-image-upload user-follow-modify user-follow-read user-library-modify user-library-read user-modify-playback-state user-read-currently-playing user-read-email user-read-playback-position user-read-playback-state user-read-private user-read-recently-played user-top-read user-personalized'.replace(' ', '%20')
redirect_url = 'https://developer.spotify.com'
login_url = f'https://accounts.spotify.com/authorize?response_type=code&client_id={client_id}&scope={scope}&code_challenge_method=S256&code_challenge={code_challenge}&redirect_uri={redirect_url}'
if args.cookies:
cookiejar = http.cookiejar.MozillaCookieJar()
cookiejar.load(args.cookies)
# TODO: disable following redirects
full_url = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookiejar)).open(login_url).url
else:
print(f'Please visit {login_url}')
print('After logging in, copy the full URL and paste it below')
full_url = input()
code = urllib.parse.parse_qs(urllib.parse.urlparse(full_url).query)['code'][0]
print(json.loads(urllib.request.urlopen('https://accounts.spotify.com/api/token', data=f'client_id={client_id}&grant_type=authorization_code&code={code}&redirect_uri={redirect_url}&code_verifier={code_verifier}'.encode()).read())['access_token'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment