Created
October 19, 2022 21:56
-
-
Save ilyador/3e259398f6f2252630bac7e2ca6d9d72 to your computer and use it in GitHub Desktop.
nextjs-tweets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import react from 'react' | |
import needle from 'needle' | |
const token = process.env.BEARER_TOKEN | |
const recentEndpointUrl = "https://api.twitter.com/2/tweets/search/recent"; | |
function Page ({ data }) { | |
console.log(data) | |
return ( | |
data.data.map(tweet => | |
<div>{tweet.text}</div> | |
) | |
) | |
} | |
export async function getServerSideProps () { | |
try { | |
const response = await getRequest(); | |
return { props: { data: response.data } } | |
} | |
catch (error) { | |
console.log(error); | |
return { props: { data: null } } | |
} | |
} | |
async function getRequest() { | |
const params = { | |
'query': 'from:twitterdev -is:retweet', | |
'tweet.fields': 'author_id' | |
} | |
const res = await needle('get', recentEndpointUrl, params, { | |
headers: { | |
"User-Agent": "v2RecentSearchJS", | |
"authorization": `Bearer ${token}` | |
} | |
}) | |
if (res.body) { | |
return res.body; | |
} else { | |
throw new Error('Unsuccessful request'); | |
} | |
} | |
export default Page |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment