I'm working on a Next.JS app which will retrieve a list of tweets by hashtag (ideally, by hashtag between a start and end date, but one step at a time.)
Sadly, this all errors out. Process.env variables work.
fetch is isomorphic-unfetch. I think the problem is that I'm just not setting the headers correctly - what do you think?
edited for latest tools.
Tasks.getInitialProps = async function() {
const authString: string = encodeURIComponent(
[
"OAuth",
`oauth_consumer_key="${process.env.TWITTER_API_KEY}",`,
`oauth_token="${process.env.TWITTER_TOKEN}",`,
`oauth_nonce="${Math.random().toString}",`,
`oauth_timestamp="${Date.now()}",`,
`oauth_signature_method="HMAC-SHA1",`,
`oauth_version="1.0"`
].join(" ")
);
try {
console.log(authString); // looks good.
const res = await fetch(
"https://api.twitter.com/1.1/search/tweets.json?q=%23hometasking",
{
method: "get",
headers: {
authorization: authString
}
}
);
const data = await res.json();
console.log(data);
return {
entries: { tweets: data }
};
} catch (err) {
console.error(err);
return { entries: { tweets: err } };
}
};
Error in the console (server side:)
{ errors: [ { code: 215, message: 'Bad Authentication data.' } ] }