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

An error occurred: Request failed with code 400 - Invalid Request: One or more parameters to your request was invalid. (see https://api.twitter.com/2/problems/invalid-request #482

Closed SeloSlav closed 1 year ago

SeloSlav commented 1 year ago

Bug Description: When attempting to use the reply function in the twitter-api-v2 library for Node.js to interact with the Twitter API, an error occurs. The error message states: "Request failed with code 400 - Invalid Request: One or more parameters to your request was invalid."

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

const client = new TwitterApi({
  appKey: process.env.NEXT_PUBLIC_TWITTER_CONSUMER_KEY,
  appSecret: process.env.NEXT_PUBLIC_TWITTER_CONSUMER_SECRET,
  accessToken: process.env.NEXT_PUBLIC_TWITTER_ACCESS_TOKEN_KEY,
  accessSecret: process.env.NEXT_PUBLIC_TWITTER_ACCESS_TOKEN_SECRET,
});

export default async function handler(req, res) {
  if (req.method === 'POST') {
    try {
      const tweetId = req.body.tweetId;

      const replyText = "This is a reply.";

      // Post the reply using Twitter API v2
      const replyResponse = await client.v2.reply(replyText, tweetId);

      if (replyResponse) {
        res.status(200).json({ success: true, replyText });
      } else {
        res.status(500).json({ success: false, message: 'Error posting the reply.' });
      }
    } catch (error) {
      console.error(error);
      res.status(500).json({ success: false, message: error.message });
    }
  } else {
    res.status(405).json({ success: false, message: 'Method Not Allowed' });
  }
}

Steps to Reproduce:

Expected Behavior: The expected behavior was a successful interaction with the Twitter API, allowing the application to post tweets, read user tweets, and perform other permitted Twitter actions based on the applied OAuth2 scopes.

Versions:

Additional Context: Due to the absence of a detailed error stack trace or specific identification of the invalid parameter, it is challenging to pinpoint the exact source of the issue. The OAuth2 setup and request parameters were implemented according to the documentation provided by the twitter-api-v2 library and the Twitter API. Therefore, the cause of the error remains unclear. Any further information or assistance in identifying the invalid parameter would be greatly appreciated to resolve this issue.