Skip to content

OAuth 2 error for POST /2/tweets FIXED #186

Description

@charlesr1971

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:

image

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions