Using javascript, I am trying to get tweets as JSON on my web page (8th wall). Here is my code
const method = 'GET'
const options = {
method,
headers: {
'Content-type': 'application/json',
'Authorization': BEARER_TOKEN,
},
}
const getData = async () => {
try {
const response = await fetch(
'https://api.twitter.com/2/users/2244994945/tweets',
options
)
const data = await response.json()
console.log(data)
} catch (error) {
console.log('ERROR', error)
}
}
function GetMyFeeds() {
console.log('GetMyFeeds')
getData()
}
nextButton.onclick = GetMyFeeds
Getting following error message
TypeError: Failed to fetch
But data fetching is working on postman
This is an error somewhere from your code, not from the API, so it’s worth investing where exactly this is coming from, and what the exact response is from the API itself.
I have tried the following function
const getData3 = async () => {
try {
const response = await fetch(
'https://reqres.in/api/users/1',
)
const data = await response.json()
console.log(data)
} catch (error) {
console.log('ERROR', error)
}
}
JSON data is received properly
Are any headers needed to add than ‘Authorization’: BEARER_TOKEN ?
nvahnav
#4
@LijuAlivenow
Make sure you have the string "Bearer " in your BEARER_TOKEN variable, although not having it throws 401 for me, not a “TypeError”:
Cheers | van_havn
1 Like
This is not the Twitter API, so the error is probably coming from there.
1 Like
same error here pass the token … on insomnia working well but on runjs for example no.
If it works in Insomnia or another tool, i would suspect there’s something wrong with the implementation you’re using - or something else is different. runjs may be missing something in the environment or doing something different or trying to call the API from the browser incorrectly - twitter API does not support CORS.
I mean that this is not code related issue. That is why I m getting Json data from the test URL(https://reqres.in/api/users/1), but no data from Twitter API URL.
I think Twitter API does not support CORS. That’s why I am not getting any JSON data.
I am trying this code in my 8th wall project. But the same URL is working in Postman
Yes, this is still the case unfortunately, it’s the most common stumbling block when working with J’s and the Twitter API.