0

I know that there is a very similar question like this one on StackOverflow, but i do not understand what the answer means, and because of that i am asking this question.

here's my code

const tokens = JSON.parse(fs.readFileSync("./tokens.json"));

const { accessToken, accessSecret, oauth_verifier } = tokens;

const client = new TwitterApi({
    appKey: process.env.API_KEY,
    appSecret: process.env.API_KEY_SECRET,
    accessToken: accessToken,
    accessSecret: accessSecret
});

const {client: Bot} = await client.login(oauth_verifier);

running this throws a 401 error with data saying Request token missing: ''.

i believe the problem lies in the oauth_verifier, as the code runs fine, and i have checked all my other credentials. I can also run a console.log() without running into errors if the last line in my code block above is commented.

  • First step would be to check `console.log({ appKey: process.env.API_KEY, appSecret: process.env.API_KEY_SECRET, accessToken: accessToken, accessSecret: accessSecret })` to verify that the data you think should be there is there. (If you post it, don't forget to anonymise it, e.g. by replacing some of the secrets' characters with `#`) – Amadan May 17 '22 at 10:36
  • @Amadan I just checked all the values, and it seems they are all correct. – Ranvir Choudhary May 17 '22 at 10:44
  • Hmm, not sure, but I think you are trying to use a 3-legged OAuth, but skipped a step. Your `accessToken` and `accessSecret` are probably stale; they are not meant to be read from a file, but requested from the API using the request token operation. See [here](https://github.com/PLhery/node-twitter-api-v2/blob/b3d8f6a44d711c16dad37968af057a193342da5f/doc/auth.md). – Amadan May 17 '22 at 10:52
  • @Amadan i have done that step. i am storing the `accessToken` and `accessSecret` in a file, like the example tells me to. i do not need a database since i am not providing a service. i am writing these to a file so i can access them in another file. – Ranvir Choudhary May 17 '22 at 11:44

1 Answers1

0

The secret for getting this to work for me was not using .login().

Just instantiate the client using the details you got from the OAuth 1.0 auth flow. No need for .login()

Jeffery Bennett
  • 490
  • 1
  • 5
  • 9