node-twitter-api-v2 icon indicating copy to clipboard operation
node-twitter-api-v2 copied to clipboard

403 error when attempting to post

Open campgurus opened this issue 2 years ago • 13 comments

Note: For questions about how to use an endpoint, or problems related to Twitter API than the lib itself, please use the GitHub Discussions instead of opening a new issue.

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();

campgurus avatar Oct 15 '23 12:10 campgurus

This often happens when a Twitter App isn't a member of a project. Head to your projects & apps overview page - if your app appears under "Standalone Apps" you'll need to create a project and assign the app to it

mwilkinson22 avatar Oct 19 '23 16:10 mwilkinson22

The App is within a project. screenshot:

Screenshot 2023-10-20 at 6 05 33 AM

campgurus avatar Oct 20 '23 10:10 campgurus

Ah sorry - I'm not affiliated with this library but I'd just had that problem myself recently, was a bit of a shot in the dark!

On closer inspection it looks like a basic authentication issue - what happens if you call twitterClient.v1.verifyCredentials()?

mwilkinson22 avatar Oct 20 '23 11:10 mwilkinson22

No worries grateful for the help. Meanwhile, I think I don't have access to v1:

ApiResponseError: Request failed with code 403 - Your credentials do not allow access to this resource (Twitter code 37)
    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 Gunzip.emit (node:events:513:28)
    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: {
    date: 'Fri, 20 Oct 2023 11:17:20 UTC',
    perf: '7626143928',
    server: 'tsa_b',
    'set-cookie': [
      'guest_id_marketing=v1%3A169780064063369209; Max-Age=63072000; Expires=Sun, 19 Oct 2025 11:17:20 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',
      'guest_id_ads=v1%3A169780064063369209; Max-Age=63072000; Expires=Sun, 19 Oct 2025 11:17:20 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',
      'personalization_id="v1_kxZM5/LOaZoQG0/8kpgZuQ=="; Max-Age=63072000; Expires=Sun, 19 Oct 2025 11:17:20 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',
      'guest_id=v1%3A169780064063369209; Max-Age=63072000; Expires=Sun, 19 Oct 2025 11:17:20 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None'
    ],
    'content-type': 'application/json;charset=utf-8',
    'cache-control': 'no-cache, no-store, max-age=0',
    'x-transaction-id': 'd3171bde74e8da0b',
    'strict-transport-security': 'max-age=631138519',
    'content-encoding': 'gzip',
    'content-length': '106',
    'x-response-time': '4',
    'x-connection-hash': 'c5163e55a1a73330ff02f2aae694e224e1645f5e29dbd0558c0eb2cb0af76eff',
    connection: 'close'
  },
  rateLimit: undefined,
  data: { errors: [ [Object] ] },
  errors: [
    {
      message: 'Your credentials do not allow access to this resource',
      code: 37
    }
  ]

campgurus avatar Oct 20 '23 11:10 campgurus

Facing the same issue here

EDIT: I resolved the issue by configuring the authentication settings and subsequently generating new API and secret keys

image

sir-kain avatar Oct 28 '23 02:10 sir-kain

image I'm also facing the same 403 error.

arunlz avatar Nov 08 '23 19:11 arunlz

I am getting the same error when I try to upload Media. Did anyone had any success with this error?

wavecatch2020s avatar Nov 22 '23 21:11 wavecatch2020s

same error here

Prottoy2938 avatar Dec 20 '23 04:12 Prottoy2938

I am getting the same error when I try to upload Media. Did anyone had any success with this error?

Did you ever figure it out? Having the same issue here 😅

solojungle avatar Mar 05 '24 19:03 solojungle

I am getting the same error when I try to upload Media. Did anyone had any success with this error?

Did you ever figure it out? Having the same issue here 😅

that makes two of us :(

cheskoxd avatar Mar 15 '24 05:03 cheskoxd

I am getting the same error when I try to upload Media. Did anyone had any success with this error?

Did you ever figure it out? Having the same issue here 😅

that makes two of us :(

@cheskoxd my solution is here!

https://github.com/PLhery/node-twitter-api-v2/discussions/416#discussioncomment-8684794

solojungle avatar Mar 18 '24 01:03 solojungle

Thanks!!

cheskoxd avatar Mar 18 '24 05:03 cheskoxd

Facing the same issue here

EDIT: I resolved the issue by configuring the authentication settings and subsequently generating new API and secret keys

image

Thanks @sir-kain I was also able to fix this issue simply by regenerating keys.

whenmoon avatar Jul 01 '24 14:07 whenmoon