fix: use sp_t cookie trick for Spotify anonymous token (spotdl approach)
This commit is contained in:
1 parent
6c4caebc69
commit
b7f09058a9
1 file changed
+12
-11
+12
-11
@@ -72,22 +72,23 @@ let _spTokenExpiry = 0;
|
|||||||
|
|
||||||
async function fetchSpotifyToken() {
|
async function fetchSpotifyToken() {
|
||||||
if (_spTokenCache && Date.now() < _spTokenExpiry) return _spTokenCache;
|
if (_spTokenCache && Date.now() < _spTokenExpiry) return _spTokenCache;
|
||||||
// Extract anonymous token from embed page — works from any IP unlike get_access_token
|
// spotdl trick: send a random sp_t cookie — Spotify requires some value but doesn't validate it
|
||||||
const r = await fetch('https://open.spotify.com/embed/track/4uLU6hMCjMI75M1A2tKUQC', {
|
const sp_t = require('crypto').randomUUID();
|
||||||
|
const r = await fetch('https://open.spotify.com/get_access_token?reason=transport&productType=web_player', {
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
|
||||||
'Accept': 'text/html,application/xhtml+xml',
|
'Accept': 'application/json',
|
||||||
'Accept-Language': 'en-US,en;q=0.9',
|
'Accept-Language': 'en-US,en;q=0.9',
|
||||||
|
'Referer': 'https://open.spotify.com/',
|
||||||
|
'Cookie': `sp_t=${sp_t}`,
|
||||||
},
|
},
|
||||||
signal: AbortSignal.timeout(12000),
|
signal: AbortSignal.timeout(10000),
|
||||||
});
|
});
|
||||||
if (!r.ok) throw new Error(`Spotify-Embed nicht erreichbar (${r.status})`);
|
if (!r.ok) throw new Error(`Spotify-Token nicht abrufbar (${r.status})`);
|
||||||
const html = await r.text();
|
const d = await r.json();
|
||||||
const tokenMatch = html.match(/"accessToken"\s*:\s*"([^"]+)"/);
|
if (!d.accessToken) throw new Error('Kein Spotify-Token erhalten');
|
||||||
if (!tokenMatch) throw new Error('Kein Token in Spotify-Embed gefunden');
|
_spTokenCache = d.accessToken;
|
||||||
const expiryMatch = html.match(/"accessTokenExpirationTimestampMs"\s*:\s*(\d+)/);
|
_spTokenExpiry = (d.accessTokenExpirationTimestampMs || (Date.now() + 55 * 60 * 1000)) - 30000;
|
||||||
_spTokenCache = tokenMatch[1];
|
|
||||||
_spTokenExpiry = (expiryMatch ? parseInt(expiryMatch[1]) : (Date.now() + 55 * 60 * 1000)) - 30000;
|
|
||||||
return _spTokenCache;
|
return _spTokenCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user