Skip to content

Instantly share code, notes, and snippets.

@ilyador
Created October 19, 2022 21:56
Show Gist options
  • Save ilyador/3e259398f6f2252630bac7e2ca6d9d72 to your computer and use it in GitHub Desktop.
Save ilyador/3e259398f6f2252630bac7e2ca6d9d72 to your computer and use it in GitHub Desktop.
nextjs-tweets
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