I am implementing the boilerplate code for recent search here https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/Recent-Search/recent_search.js.
I am using the bearer token i have generated. this token works fine when i use it to make the api call on postman. However, locally, it return a 401 unauthorized error. I am not sure.
The only additional bit I have done, is use dotenv package and created an env file where the bear token is stored. For this, i have had to require the dotenv package in the index.js.
require(“dotenv”).config()
This is accessible via process.env and I can print out the bearer token fine. So not sure where the issue is here.
help would be appreciated.
I would double check that the token in the env file was pasted in properly without any extra characters or something, and is actually loaded at run time, but if it definitely prints out correctly i’ve no idea.
Thanks for responding. I have checked and token has been added correctly. Essentially i have got the following in the .env file
BEARER_TOKEN= 'xyz ’
xyz is the token straight from the twitter developer portal
That same token is working on postman.
Do you reckon its to do with how i have set up the authentication setting in twitter developer portal. the bearer token uses o auth 2.0. The are some general auth setting which require a URL. do you reckon this is where the issue is stemming from?
Is there actually a space at the end of the bearer token ? Or a different kind of quote? That matters I think, so I’d double check that maybe, maybe the quotes are being included? Or maybe there is a % in the token that’s incorrectly parsed as a variable in the env file?
The callback url has no bearing on an already generated Bearer token so it’s not that.
If you are running on wsl (linux sub-system) or VM, it could be your date & time. Try synchronizing time by running sudo hwclock -s and then sudo ntpdate pool.ntp.org
1 Like