question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unauthorized when POST to Tweets endpoint

See original GitHub issue

https://twittercommunity.com/t/post-request-returning-error-401-unauthorized/162950/4

following the example in the README, let response = await client.get('tweets', { ids: id }) works as expected, but let response = await client.post('tweets', { text: message }) fails with { code: 401, details: 'Unauthorized' }. nodejs 17.2.0, windows.

It’s expected that POST should authenticate using the same method and send a tweet successfully.

Full code example:

const Twitter = require("twitter-v2")

const client = new Twitter({
  consumer_key: process.env.TWITTER_CONSUMER_KEY,
  consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
  access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});

async function sendTweet(message) {
  try {
    const response = await client.post('tweets', { text: message })
    console.log(response)
  } catch (err) {
    console.error(err)
  }
}

async function getTweet(id) {
  try {
    const response = await client.get('tweets', { ids: id })
    console.log(response)
  } catch (err) {
    console.error(err)
  }
}

sendTweet('test tweet!')

getTweet('1228393702244134912')

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:8

github_iconTop GitHub Comments

2reactions
ssplattcommented, Dec 9, 2021

I changed to twitter-api-v2 and that works perfectly

0reactions
ftemaricommented, Jul 3, 2022

I found it. I was confusing about the user flow authentication. But how i will using my own account its not necessary!

Thanks for the response men!

Read more comments on GitHub >

github_iconTop Results From Across the Web

POST request returning ERROR 401 "Unauthorized"
There was a problem authenticating your request. This could be due to missing or incorrect authentication credentials. This may also be returned ...
Read more >
Twitter API returned a 401 (Unauthorized), An error ...
After running it I receive the following error: python tweet.py previous_cursor previous_cursor_str next_cursor ids next_cursor_str Twitter API ...
Read more >
Twitter API Response Codes & Error Support
Corresponds with HTTP 403. Thrown when a Tweet cannot be posted due to the user having no allowance remaining to post. Despite the...
Read more >
Error 401, "unauthorized" while trying to connect to Twitter's ...
You have the right idea in terms of getting started. A 401 error usually mean there was a problem authenticating your request. From...
Read more >
Not able to hit Twitter API through POSTMAN - Microsoft Q&A
Error: When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found