Skip to content

Instantly share code, notes, and snippets.

@atkinson
Created October 24, 2023 22:15
Show Gist options
  • Save atkinson/293661ab442f075222ac6c2d7dac4353 to your computer and use it in GitHub Desktop.
Save atkinson/293661ab442f075222ac6c2d7dac4353 to your computer and use it in GitHub Desktop.
Client Credentials OAUTH for Spotify
import os
import requests
### This flow does not include user authorization,
### so only endpoints that do not request user information
### (e.g. user profile data) can be accessed.
### https://developer.spotify.com/documentation/web-api/concepts/authorization
SPOTIFY_CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID")
SPOTIFY_CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRET")
CREDENTIALS = base64.b64encode(
f"{SPOTIFY_CLIENT_ID}:{SPOTIFY_CLIENT_SECRET}".encode("ascii")
).decode("ascii")
res = requests.post(
url="https://accounts.spotify.com/api/token",
headers={
"Authorization": f"Basic {CREDENTIALS}",
"Content-Type": "application/x-www-form-urlencoded",
},
data={"grant_type": "client_credentials"},
)
print(res.json())
ACCESS_TOKEN = res.json()["access_token"]
res = requests.get(
url=f"https://api.spotify.com/v1/playlists/{PLAYLIST_ID}",
headers={
"Authorization": f"Bearer {ACCESS_TOKEN}",
"Content-type": "application/json",
"Accept": "text/plain",
},
)
print(res.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment