PLhery / node-twitter-api-v2

Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.
https://www.npmjs.com/package/twitter-api-v2
Apache License 2.0
1.32k stars 186 forks source link

403 error when attempting to post #500

Open campgurus opened 1 year ago

campgurus commented 1 year ago

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();
mwilkinson22 commented 1 year ago

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

campgurus commented 1 year ago

The App is within a project. screenshot:

Screenshot 2023-10-20 at 6 05 33 AM
mwilkinson22 commented 1 year ago

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

campgurus commented 1 year ago

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
    }
  ]
sir-kain commented 1 year ago

Facing the same issue here

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

image

arunlz commented 1 year ago

image I'm also facing the same 403 error.

wavecatch2020s commented 1 year ago

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

Prottoy2938 commented 11 months ago

same error here

solojungle commented 9 months ago

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 πŸ˜…

cheskoxd commented 8 months ago

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 :(

solojungle commented 8 months ago

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

cheskoxd commented 8 months ago

Thanks!!

whenmoon commented 5 months ago

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.