Estoy tratando de obtener la identificación del tweet como entrada de formulario y luego uso la API de Twitter para consultar datos sobre ese tweet en particular. El problema es que, por alguna razón, los datos JSON no regresan correctamente.
router.post('/', async function(req, res, next) { let tweet_id = req.body.tweet_id; //'1466189950135640065'; console.log(tweet_id); let tweet = await fetch(`https://api.twitter.com/2/tweets/${tweet_id}`, { method: 'post', headers: new Headers({ 'Authorization': `Bearer ${BEARER_TOKEN}`, }) }) .then(res => res.json()) .then(json => console.log(json)) .catch(err => console.log(err)); res.render('index'); });El código anterior produce el siguiente mensaje de error
FetchError: invalid json response body at https://api.twitter.com/2/tweets/1036132920556232705 reason: Unexpected token A in JSON at position 0 Creo que tiene que ver con el hecho de que estoy consumiendo el cuerpo de la respuesta como JSON y el flujo de respuesta se detiene allí. Pero incluso si comento .then(res => res.json()) obtengo algo en mi console.log pero no veo el resultado esperado.
debería lucir así
{"data":{"id":"1036132920556232705","text":"Other sites of yours truly to follow...."}}En cambio, obtengo algo completamente diferente.
Response { size: 0, timeout: 0, [Symbol(Body internals)]: { body: Gunzip { _writeState: [Uint32Array], _readableState: [ReadableState], _events: [Object: null prototype], _eventsCount: 5, _maxListeners: undefined, _writableState: [WritableState], allowHalfOpen: true, bytesWritten: 0, _handle: [Zlib], _outBuffer: <Buffer 41 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 70 65 72 66 6f 72 6d 20 77 72 69 74 65 20 61 63 74 69 6f 6e 73 2e 20 43 6f 6e 74 61 63 74 20 ... 16334 more bytes>, _outOffset: 0, _chunkSize: 16384, _defaultFlushFlag: 2, _finishFlushFlag: 2, _defaultFullFlushFlag: 3, _info: undefined, _maxOutputLength: 4294967296, _level: -1, _strategy: 0, [Symbol(kCapture)]: false, [Symbol(kCallback)]: null, [Symbol(kError)]: null }, disturbed: false, error: null }, [Symbol(Response internals)]: { url: 'https://api.twitter.com/2/tweets/1036132920556232705', status: 403, statusText: 'Forbidden', headers: Headers { [Symbol(map)]: [Object: null prototype] }, counter: 0 } } No veo las claves de data , id o text ningún lado. ¿Qué estoy haciendo mal aquí?
A menos que Phil quiera dar una respuesta, que aceptaré con gusto, mi problema fue que había establecido el method: POST en lugar de GET para la llamada a la API. Fue un error tonto.