Spotify Oauth2 Docs - Flujo de credenciales del cliente
Hice una pregunta similar y pude ejecutar el código en el script de aplicaciones de Google (javascript). Estoy usando Flujo de credenciales de cliente. Sin embargo, traté de recrear este código en Nodejs. Al ejecutar este código, recibo un error del servidor.
error: "server_error"Este es el código del problema en Nodejs
const defaultConfig = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', // this encodes the app_id and api_key into a base64 string like the documentation states 'Authorization': `Basic ${btoa(spotify.API_ID + ':' + spotify.API_KEY)}` }, body: JSON.stringify({ 'grant_type': 'client_credentials', }) }; // this function is wrapped in a list titled, "apiSettings" with other functions. I just included the authorization function here: const apiSettings = { getSpotifyAuthorizeToken: async () => { const endpoint = "https://accounts.spotify.com/api/token"; return await (await fetch(endpoint, defaultConfig)).json(); }, };Obtuve este código similar para trabajar en el script de aplicaciones de Google sin problemas.
¿Podría el error del servidor ser el resultado de que ejecuté Nodejs en un "localhost: xxxx"? La documentación establece que "el flujo de credenciales de cliente se usa en la autenticación de servidor a servidor".
si este no es el caso, ¿podría proporcionar un trabajo
Respuesta de error del servidor Spotify API OAUTH2 al solicitar token
De su pregunta anterior , me gustaría proponer los siguientes scripts de muestra.
Si desea convertir el script de esta respuesta a Node.js y node-fetch, ¿qué tal el siguiente script?
const spotify = { API_ID: "API_ID", API_KEY: "API_KEY" }; // Please set your client ID and client secret. const buf = Buffer.from(spotify.API_ID + ":" + spotify.API_KEY); const para = new URLSearchParams(); para.append("grant_type", "client_credentials"); const defaultConfig = { method: "POST", headers: { Authorization: `Basic ${buf.toString("base64")}` }, body: para, }; const apiSettings = { getSpotifyAuthorizeToken: async () => { const endpoint = "https://accounts.spotify.com/api/token"; return await (await fetch(endpoint, defaultConfig)).json(); }, };Si desea convertir el script de esta respuesta a Node.js y node-fetch, ¿qué tal el siguiente script?
const spotify = { API_ID: "API_ID", API_KEY: "API_KEY" }; // Please set your client ID and client secret. const para = new URLSearchParams(); para.append("grant_type", "client_credentials"); para.append("client_id", spotify.API_ID); para.append("client_secret", spotify.API_KEY); const defaultConfig = { method: "POST", body: para, }; const apiSettings = { getSpotifyAuthorizeToken: async () => { const endpoint = "https://accounts.spotify.com/api/token"; return await (await fetch(endpoint, defaultConfig)).json(); }, };