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

[bug]ApiResponseError: Request failed with code 403

Open spencer17x opened this issue 1 year ago • 5 comments

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/[email protected]/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/[email protected]/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

  • Node.js version: v16.17.0
  • Lib version: 1.16.0

spencer17x avatar Feb 01 '24 13:02 spencer17x

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

satyamy846 avatar Feb 24 '24 07:02 satyamy846

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.

theskyvalker avatar Feb 24 '24 15:02 theskyvalker

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

satyamy846 avatar Feb 24 '24 18:02 satyamy846

Any progress ? @theskyvalker

awais2015 avatar Mar 18 '24 09:03 awais2015

Unfortunately, this is most likely a limitation of the free plan. To check this, you can use this method:

async function getMyAccountInfo() {
    try {
        const user = await client.v2.me();
        console.log(user);
    } catch (error) {
        console.error('Error fetching account info:', error);
    }
}

getMyAccountInfo();

If you get the correct answer (Own account), then the problem is with your subscription

SamuiRo avatar Jul 30 '24 04:07 SamuiRo