twitter-v2
twitter-v2 copied to clipboard
Unauthorized when POST to Tweets endpoint
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')
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 Were you able to figure it out? I'm having the same issue
I changed to twitter-api-v2 and that works perfectly
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!
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
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!
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
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!