Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
What is the meaning of these errors I received when using the Twitter API?

Here is my code, I am trying to make an AI Twitter Bot using the Twitter API & Firebase:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const dbRef = admin.firestore().doc('tokens/cvstate');
const dbRef2 = admin.firestore().doc('tokens/access')

const TwitterApi = require('twitter-api-v2').default;
const twitterClient = new TwitterApi({
  clientId: 'MY_ID',
  clientSecret: 'MY_SECRET',
});

const callbackURL = 'MY CALLBACK URL';

const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
  organization: 'MY ORG',
  apiKey: 'MY KEY',
});
const openai = new OpenAIApi(configuration);

exports.auth = functions.https.onRequest(async (request, response) => {
  const { url, codeVerifier, state } = twitterClient.generateOAuth2AuthLink(
    callbackURL,
    { scope: ['tweet.read', 'tweet.write', 'users.read', 'offline.access'] }
  );
  await dbRef.set({ codeVerifier, state });

  response.redirect(url);
});

exports.callback = functions.https.onRequest(async (request, response) => {
  const { state, code } = request.query;

  const dbSnapshot = await dbRef.get();
  const { codeVerifier, state: storedState } = dbSnapshot.data();

  if (state !== storedState) {
    return response.status(400).send('Stored tokens not match.');
  }

  const {
    client: loggedClient,
    accessToken,
    refreshToken,
  } = await twitterClient.loginWithOAuth2({
    code,
    codeVerifier,
    redirectUri: callbackURL,
  });

  await dbRef2.set({ accessToken, refreshToken });

  const { data } = await loggedClient.v2.me(); 

  response.send(data);
});

exports.newTweet = functions.https.onRequest(async (request, response) => {
  const { refreshToken } = (await dbRef2.get()).data();

  const {
    client: refreshedClient,
    accessToken,
    refreshToken: newRefreshToken,
  } = await twitterClient.refreshOAuth2Token(refreshToken);

  await dbRef2.set({ accessToken, refreshToken: newRefreshToken });

  const newTweet = await openai.createCompletion('text-davinci-001', {
    prompt: 'tweet something political',
    max_tokens: 64,
  });

  const { data } = await refreshedClient.v2.tweet(
    newTweet.data.choices[0].text
  );

  response.send(data);
});

Auth & Callback commands work, however when I run the auth command I get the following error message:

Error: Request failed with status code 401 at createError (/home/cog9/TwitterBot/functions/node_modules/axios/lib/core/createError.js:16:15) at settle (/home/cog9/TwitterBot/functions/node_modules/axios/lib/core/settle.js:17:12) at IncomingMessage.handleStreamEnd (/home/cog9/TwitterBot/functions/node_modules/axios/lib/adapters/http.js:322:11) at IncomingMessage.emit (node:events:402:35) at endReadableNT (node:internal/streams/readable:1343:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!