twitter-lite: OAuth 2 error for POST /2/tweets FIXED
I am using the latest version of twitter-lite
I thought we were using oAuth 1.0a, but I get this error:
{
_headers: Headers {
[Symbol(map)]: [Object: null prototype] {
'set-cookie': [Array],
'content-type': [Array],
'cache-control': [Array],
'content-length': [Array],
'x-response-time': [Array],
'x-connection-hash': [Array],
date: [Array],
server: [Array],
connection: [Array]
}
},
title: 'Unsupported Authentication',
detail: 'Authenticating with OAuth 2.0 Application-Only is forbidden for this endpoint. Supported authentication types are [OAuth 1.0a User Context, OAuth 2.0 User Context].',
type: 'https://api.twitter.com/2/problems/unsupported-authentication',
status: 403
}
Here is the code
async function createNewTwitterClient (subdomain, extension, response, twitterAppConfig, version) {
const twitter = require("twitter-lite");
return new twitter({
subdomain,
consumer_key: twitterAppConfig.consumerKey, // from Twitter.
consumer_secret: twitterAppConfig.consumerSecret,
extension, // true is the default (this must be set to false for v2 endpoints),
access_token_key: twitterAppConfig.accessTokenKey, // from your User (oauth_token)
access_token_secret: twitterAppConfig.accessTokenSecret, // from your User (oauth_token_secret)
bearer_token: response ? response?.access_token : null,
version
});
}
const apiClient = await createNewTwitterClient('api', false, null, twitterAppConfig,'2');
const apiResponse = await apiClient.getBearerToken();
const twitterApiClient = await createNewTwitterClient('api', false, apiResponse, twitterAppConfig,'2');
twitterApiClient.post('tweets', { text: 'hello world' }).then(tweet => {
// do something
}).catch();
In Postman, it works fine:

About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 15
All I know is that the current Twitter-lite V2
/tweetsendpoint does NOT work.My PR code updates, work with me.
I must say your content type error message, is a little strange.
Response:
any fix? @charlesr1971
Chiming in here to say that I’m also receiving the same content-type error for the
/tweetsendpoint.Reponds with:
@charlesr1971 I will take a look at your PR!
Yes. You need to use the twitter-lite version in my PR request above:
https://github.com/draftbit/twitter-lite/pull/187
Unfortunately, the owners of the twitter-lite repo, are not merging PR requests at the moment.
But, you can clone my repository and use that:
https://github.com/charlesr1971/twitter-lite/tree/%40charlesr1971
God, this MarkDown Editor is terrible. You would think GitHub could sort this out? 😩
I have found the fix:
Oh. And I had to update my code as well:
Here is the code
So, I removed the bearer token from the twitterAppConfig
Maybe, you should be allowed to send the bearer token, along with the access token, without losing the ability to POST. Currently, your code chooses which type of authentication to use, based on whether the bearer token is present. It might be better to choose which type of authentication to use, based on whether the bearer token is present and whether the access token is not present?