Get 403 error when attempting to post to twitter from node app with twitter-api-v2

87 views Asked by At

Trying to create a Twitter bot in Node.js. Getting a 403 error on the request.

here is the full error trace:

ApiResponseError: Request failed with code 403
    at RequestHandlerHelper.createResponseError (/Users/darius/Code/flumly-bot/node_modules/twitter-api-v2/dist/cjs/client-mixins/request-handler.helper.js:104:16)
    at RequestHandlerHelper.onResponseEndHandler (/Users/darius/Code/flumly-bot/node_modules/twitter-api-v2/dist/cjs/client-mixins/request-handler.helper.js:262:25)
    at IncomingMessage.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  error: true,
  type: 'response',
  code: 403,
  headers: {
    perf: '7626143928',
    'content-type': 'application/problem+json',
    'cache-control': 'no-cache, no-store, max-age=0',
    'content-length': '297',
    'x-transaction-id': '210b23a7a4b6ca3c',
    'x-response-time': '2',
    'x-connection-hash': '5a7ce993b8ccc4f087354980c7c7c510c2ded8b91ab6000756d91ded7cd7c16c',
    date: 'Sun, 15 Oct 2023 11:33:38 GMT',
    server: 'tsa_b',
    connection: 'close'
  },
  rateLimit: undefined,
  data: {
    title: 'Unsupported Authentication',
    detail: 'Authenticating with Unknown 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 my client config:

const { TwitterApi } = require("twitter-api-v2");

const client = new TwitterApi({
    appKey: process.env.TWITTER_API_KEY,
    appSecret: process.env.TWITTER_API_SECRET,
    accessToken: process.env.TWITTER_ACCESS_TOKEN,
    accessSecret: process.env.TWITTER_ACCESS_SECRET,
});

const bearer = new TwitterApi(process.env.TWITTER_BEARER_TOKEN);

const twitterClient = client.readWrite;
const twitterBearer = bearer.readOnly;

module.exports = { twitterClient, twitterBearer };

and index.js

require("dotenv").config({ path: __dirname + "/.env" });
const { twitterClient } = require("./twitterClient.js")
const tweet = async () => {
    try {
        await twitterClient.v2.tweet("Hello world!");
    } catch (e) {
        console.log(e)
    }
}

tweet();
0

There are 0 answers