Estoy tratando de hacer un sistema de inicio de sesión con discord para mi sitio web que está hecho con express. Hice una función para obtener un token de acceso para poder usar esa función en la ruta.
Estoy tratando de obtener un token de acceso de: https://discord.com/api/oauth2/token
Aquí está mi código:
async GetToken(code) { let access_token; const payload = { 'client_id': client_id, 'client_secret': client_secret, 'grant_type': 'authorization_code', 'code': code, 'redirect_uri': redirect_uri, 'scope': scope, }; const config = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, }; fetch(discord_token_url, { method: 'post', body: payload, headers: config.headers, }).then(response => response.json()).then(json => console.log(json)).catch(err => console.log(err)); return access_token; },Y aquí está el error que me sale:
{ error: 'unsupported_grant_type', error_description: 'Grant type None is not supported' }Como puede ver, he dado el tipo de subvención correcto pero recibo este error.
Olvidé actualizar para agregar la solución y vi a mucha gente mirando esto, así que aquí está la solución (gracias a @Kira): Tienes que usar URLSearchParams
// Modules const fetch = require('node-fetch'); const { url } = require('inspector'); const { URLSearchParams } = require('url'); // Add the parameters const params = new URLSearchParams(); params.append('client_id', client_id); params.append('client_secret', client_secret); params.append('grant_type', 'authorization_code'); params.append('code', code); params.append('redirect_uri', redirect_uri); params.append('scope', scope); // Send the request fetch('https://discord.com/api/oauth2/token', { method: 'post', body: params, headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' }, }).then(r => r.json()).then(Response => { // Handle it... handle() });