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.29k stars 176 forks source link

ApiResponseError: Request failed with code 401 #425

Closed Romainua closed 1 year ago

Romainua commented 1 year ago

I’m new on this SDK and Twitter API, sorry but I want to find help here. What am I doing wrong? I Have an error ApiResponseError: Request failed with code 401

  type: 'response',
  code: 401,
  headers: {
    perf: '7626143928',
    'content-type': 'application/problem+json',
    'cache-control': 'no-cache, no-store, max-age=0',
    'content-length': '99',
    'x-transaction-id': '409ca989c17a0eed',
    'x-response-time': '84',
    'x-connection-hash': 'edc80c7c35292d564c5ef2b5e11c9d0679a94319a9d863f88bf0cafabe0f5f4f',
    date: 'Thu, 22 Dec 2022 16:42:09 GMT',
    server: 'tsa_f',
    connection: 'close'
  },
  rateLimit: undefined,
  data: {
    title: 'Unauthorized',
    type: 'about:blank',
    status: 401,
    detail: 'Unauthorized'
  }
}

My code is

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

const userClient = new TwitterApi({
    accessToken: ‘895872753-someM3pahsagUaCJ0Vaa690jNeoV',
    accessSecret: 'bSecretHtNVbLS0O811tKNj',
});

const jsTweets = await userClient.v2.search('JavaScript', { 'media.fields': 'url' });

for await (const tweet of jsTweets) {
    console.log(tweet);
}
alkihis commented 1 year ago

Hi, This place is not meant to ask questions, even more when this is about Twitter API general usage. Twitter has a website for that.

And, base consumer tokens are missing from your client init.

nanthakumaran-s commented 1 year ago

@Romainua Getting the same error and trying different approaches. Have you solved this issue?

Romainua commented 1 year ago

@Romainua Getting the same error and trying different approaches. Have you solved this issue?

Be sure that data is correct. And check it on your dev twitter account API Key == APP Key API Key Secret == APP Secret Key Access token == Access token Access Token Secret == accessSecret

ViLourenco commented 1 year ago

I'm getting the same error... Have you guys find something useful?