I am trying to get the tweet id as form input, and then use the twitter API to query data about that particular tweet. The problem is, for some reason the JSON data is not returning properly.
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');
});
The above code yields the following error message
FetchError: invalid json response body at https://api.twitter.com/2/tweets/1036132920556232705 reason: Unexpected token A in JSON at position 0
I think it has to do with the fact that I'm consuming the response body as JSON and the response stream stops there. But even if I comment out the .then(res => res.json()) I do get something in my console.log but I don't see the expected output.
I should look like this
{"data":{"id":"1036132920556232705","text":"Other sites of yours truly to follow...."}}
Instead I get something completely different
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
}
}
I don't see the data, id, or text keys anywhere. What am I doing wrong here?
Unless Phil wants to make an answer, which I will gladly accept, my problem was I had set method: POST instead of GET for the API call. It was a silly mistake.