stuff/spotify/get-tokens.sh

44 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eu
if [[ -z "${BEARER_TOKEN:-}" ]]; then
printf "Atempting to scrape a bearer token...\n"
BEARER_TOKEN_RES=$(
curl -sL https://open.spotify.com/ \
| grep 'id="session"' \
| sed 's&.*<script id="session"[^>]*>&&' \
| sed 's/<.*//'
)
BEARER_TOKEN=$(echo "$BEARER_TOKEN_RES" | jq -r .accessToken)
if [[ -z "${BEARER_TOKEN:-}" ]]; then
printf "ERR: Failed to get a bearer token, pass one with \$BEARER_TOKEN\n" >&2
exit 1
fi
export BEARER_TOKEN
fi
if [[ -z "${TOKEN:-}" ]]; then
printf "Atempting to get a token with the bearer one...\n"
GUEST_TOKEN_RES=$(
curl -sX POST https://clienttoken.spotify.com/v1/clienttoken \
-H "Accept: application/json" -H "content-type: application/json" \
--data '{"client_data":{"client_version":"1.2","client_id":"d8a5ed958d274c2e8ee717e6a4b0971d","js_sdk_data":{"device_brand":"unknown","device_model":"unknown","os":"unknown","os_version":"unknown","device_id":"unknown","device_type":"unknown"}}}'
)
if ! [[ "$(echo "$GUEST_TOKEN_RES" | jq -r .response_type)" == "RESPONSE_GRANTED_TOKEN_RESPONSE" ]]; then
printf "\x1b[1;31m%s\x1b[0m\n" 'Token response is NOT granted' >&2
exit 1;
fi
TOKEN=$(echo "$GUEST_TOKEN_RES" | jq -r .granted_token.token)
if [[ -z "${BEARER_TOKEN:-}" ]]; then
printf "ERR: Failed to get a normal token, pass one with \$TOKEN\n" >&2
exit 1
fi
export TOKEN
fi