Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unauthorized when POST to Tweets endpoint #96

Open
ssplatt opened this issue Dec 2, 2021 · 8 comments
Open

Unauthorized when POST to Tweets endpoint #96

ssplatt opened this issue Dec 2, 2021 · 8 comments

Comments

@ssplatt
Copy link

ssplatt commented Dec 2, 2021

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
Copy link
Author

ssplatt commented Dec 3, 2021

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

@liviaalmeida
Copy link

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

@ssplatt
Copy link
Author

ssplatt commented Dec 9, 2021

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

@ftemari
Copy link

ftemari commented Jul 3, 2022

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!

@ssplatt
Copy link
Author

ssplatt commented Jul 3, 2022

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

@ftemari
Copy link

ftemari commented Jul 3, 2022

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!

@ssplatt
Copy link
Author

ssplatt commented Jul 3, 2022

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

@ftemari
Copy link

ftemari commented 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants