twitter-v2 icon indicating copy to clipboard operation
twitter-v2 copied to clipboard

Unauthorized when POST to Tweets endpoint

Open ssplatt opened this issue 3 years ago • 8 comments

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')

ssplatt avatar Dec 02 '21 21:12 ssplatt

I confirmed with postman that my credentials do work, and the end point is correct. So there must be something in this module. From what I can see, the auth headers should be the same as the GET request. And the BODY should be converted into JSON properly. I'm not sure where else to look right now

ssplatt avatar Dec 03 '21 21:12 ssplatt

@ssplatt Were you able to figure it out? I'm having the same issue

liviaalmeida avatar Dec 09 '21 00:12 liviaalmeida

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

ssplatt avatar Dec 09 '21 00:12 ssplatt

Hi @ssplatt ! can u share an snipet code using twitter-api-v2 creating a tweet? I have the same issue! and always receiving unauthorized!

Thanks!

ftemari avatar Jul 03 '22 04:07 ftemari

https://twittercommunity.com/t/oauth-1-0-with-postman-to-get-single-tweet-and-following-a-user-by-id-with-twitter-api-v2-0-essential/163033/6

This is how I got the auth to work. Then I just used the sample code from the repo

ssplatt avatar Jul 03 '22 13:07 ssplatt

But were did u get Access and token secret? you need to do the 3-legged auth flow ? Because im building a API so it wouldn't have interaction with the user. I want to sent a tweet on my own account

Thanks!

ftemari avatar Jul 03 '22 19:07 ftemari

But were did u get Access and token secret? you need to do the 3-legged auth flow ? Because im building a API so it wouldn't have interaction with the user. I want to sent a tweet on my own account

Thanks!

You get those from Twitter in the developers area when you sign up

ssplatt avatar Jul 03 '22 19:07 ssplatt

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!

ftemari avatar Jul 03 '22 19:07 ftemari