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.24k stars 174 forks source link

[bug]ApiResponseError: Request failed with code 403 #515

Open spencer17x opened 5 months ago

spencer17x commented 5 months ago

Describe the bug response: When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal.

To Reproduce code:

import { TwitterApi } from 'twitter-api-v2';

async function main() {
    const userClient = new TwitterApi({
        appKey: 'xxx',
        appSecret: 'xxxx',
        accessToken: 'x',xxx
        accessSecret: 'xxxx',
    });
    const result = await userClient.v2.userByUsername('TwitterDev');
    console.log('result', result);
}

main().catch(console.error);

response:

ApiResponseError: Request failed with code 403
    at RequestHandlerHelper.createResponseError (/Users/17a/Desktop/project/bots/telegram/node_modules/.pnpm/twitter-api-v2@1.16.0/node_modules/twitter-api-v2/dist/cjs/client-mixins/request-handler.helper.js:104:16)
    at RequestHandlerHelper.onResponseEndHandler (/Users/17a/Desktop/project/bots/telegram/node_modules/.pnpm/twitter-api-v2@1.16.0/node_modules/twitter-api-v2/dist/cjs/client-mixins/request-handler.helper.js:262:25)
    at Gunzip.emit (node:events:513:28)
    at Gunzip.emit (node:domain:489:12)
    at endReadableNT (node:internal/streams/readable:1358:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  error: true,
  type: 'response',
  code: 403,
  headers: {
    date: 'Thu, 01 Feb 2024 13:45:53 UTC',
    perf: '7469935968',
    server: 'tsa_m',
    'set-cookie': [
      'guest_id_marketing=v1%3A170679515311376886; Max-Age=63072000; Expires=Sat, 31 Jan 2026 13:45:53 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',
      'guest_id_ads=v1%3A170679515311376886; Max-Age=63072000; Expires=Sat, 31 Jan 2026 13:45:53 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',
      'personalization_id="v1_qfM9HswPc96c/fd6icKseQ=="; Max-Age=63072000; Expires=Sat, 31 Jan 2026 13:45:53 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',
      'guest_id=v1%3A170679515311376886; Max-Age=63072000; Expires=Sat, 31 Jan 2026 13:45:53 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None'
    ],
    'api-version': '2.92',
    'content-type': 'application/json; charset=utf-8',
    'cache-control': 'no-cache, no-store, max-age=0',
    'content-length': '329',
    'x-access-level': 'read',
    'x-frame-options': 'SAMEORIGIN',
    'content-encoding': 'gzip',
    'x-transaction-id': 'ad068621ddb126e2',
    'x-xss-protection': '0',
    'x-rate-limit-limit': '1200000',
    'x-rate-limit-reset': '1706795863',
    'content-disposition': 'attachment; filename=json.json',
    'x-content-type-options': 'nosniff',
    'x-rate-limit-remaining': '1199997',
    'strict-transport-security': 'max-age=631138519',
    'x-response-time': '107',
    'x-connection-hash': 'dafea14efab011922268976424128f92de5f71454ea00f8e1458b9668c185559',
    connection: 'close'
  },
  rateLimit: { limit: 1200000, remaining: 1199997, reset: 1706795863 },
  data: {
    client_id: '28360917',
    detail: 'When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal.',
    registration_url: 'https://developer.twitter.com/en/docs/projects/overview',
    title: 'Client Forbidden',
    required_enrollment: 'Appropriate Level of API Access',
    reason: 'client-not-enrolled',
    type: 'https://api.twitter.com/2/problems/client-forbidden'
  }
}

Expected behavior 200

Version

satyamy846 commented 4 months ago

Are you using correct credentials for authentication?, seems you might be using incorrect authentication keys. Please verify once appKey: 'xxx', // it is same API key in consumers key section of twitter developer portal appSecret: 'xxxx', // it is same API secret in consumers key section of twitter developer portal accessToken: 'x',xxx // it is same as access token in Access tokens section of twitter developer portal accessSecret: 'xxxx',// it is same as access secret in Access tokens section of twitter developer portal

theskyvalker commented 4 months ago

I am getting the same error and I am using the exact mapping that you have mentioned right now. It either gives a 89 error saying bad token if I also add the bearerToken or otherwise just the 403.

satyamy846 commented 4 months ago

@theskyvalker please connect me on linkedin if you would really want me to help. https://www.linkedin.com/in/satyam-kumar-883190158/

awais2015 commented 3 months ago

Any progress ? @theskyvalker