I'm exploring the Twitter API integrated with Nodejs, currently trying to post a tweet using the example provided "Twitter-API-v2-sample-code/Manage-Tweets/create_tweet.js" but getting error "Bad request".
I only changed the consumer_key and consumer_secret with mine.
To Reproduce
- Use "Twitter-API-v2-sample-code/Manage-Tweets/create_tweet.js" file code.
- replace consumer_key and consumer_secret with the needed values.
- run.
Expected behavior
get the "Hello World" tweet posted successfully.
Output
GotError [HTTPError]: Response code 400 (Bad Request)
at EventEmitter. (/Users/samaael-komy/IBM Unify LC2/git/Emma-General-BE/node_modules/got/source/as-promise.js:74:19)
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
host: 'api.twitter.com',
hostname: 'api.twitter.com',
method: 'POST',
path: '/2/tweets',
socketPath: undefined,
protocol: 'https:',
url: 'https://api.twitter.com/2/tweets',
gotOptions: {
path: '/2/tweets',
protocol: 'https:',
slashes: true,
auth: null,
host: 'api.twitter.com',
port: null,
hostname: 'api.twitter.com',
hash: null,
search: null,
query: null,
pathname: '/2/tweets',
href: 'https://api.twitter.com/2/tweets',
retry: {
retries: [Function (anonymous)],
methods: [Set],
statusCodes: [Set],
errorCodes: [Set]
},
headers: {
'user-agent': 'v2CreateTweetJS',
authorization: 'OAuth oauth_consumer_key="XXX", oauth_nonce="XXX", oauth_signature="XXX", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1642421266", oauth_token="XXX", oauth_version="1.0"',
'content-type': 'application/json',
accept: 'application/json',
'accept-encoding': 'gzip, deflate'
},
hooks: {
beforeRequest: [],
beforeRedirect: [],
beforeRetry: [],
afterResponse: [],
beforeError: [],
init: []
},
decompress: true,
throwHttpErrors: true,
followRedirect: true,
stream: false,
form: false,
json: { text: 'Hello world!' },
cache: false,
useElectronNet: false,
responseType: 'json',
method: 'POST'
},
statusCode: 400,
statusMessage: 'Bad Request',
headers: {
date: 'Mon, 17 Jan 2022 12:07:47 UTC',
server: 'tsa_f',
'set-cookie': [
'guest_id_marketing=XXX; Max-Age=63072000; Expires=Wed, 17 Jan 2024 12:07:47 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',
'guest_id_ads=XXX; Max-Age=63072000; Expires=Wed, 17 Jan 2024 12:07:47 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',
'personalization_id="XXX"; Max-Age=63072000; Expires=Wed, 17 Jan 2024 12:07:47 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',
'guest_id=XXX; Max-Age=63072000; Expires=Wed, 17 Jan 2024 12:07:47 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None'
],
'api-version': '2.34',
'content-type': 'application/json; charset=utf-8',
'cache-control': 'no-cache, no-store, max-age=0',
'content-length': '187',
'x-access-level': 'read-write',
'x-frame-options': 'SAMEORIGIN',
'content-encoding': 'gzip',
'x-xss-protection': '0',
'x-rate-limit-limit': '200',
'x-rate-limit-reset': '1642421945',
'content-disposition': 'attachment; filename=json.json',
'x-content-type-options': 'nosniff',
'x-rate-limit-remaining': '197',
'strict-transport-security': 'max-age=631138519',
'x-response-time': '147',
'x-connection-hash': '2863900f313c672dd21c6ea0b1ba9b1f80b4ef8de9bc20aa2d5bfaa350f10016',
connection: 'close'
},
body: {
errors: [ [Object] ],
title: 'Invalid Request',
detail: 'One or more parameters to your request was invalid.',
type: 'https://api.twitter.com/2/problems/invalid-request'
}
}
Have you tried using a previous version of got? see: #76
I also suggest you check const id = "...";
and be sure your user ID value is correct. You can find a user ID by using the user lookup endpoint.
I did try to run this code and found that it did not work. The error I got was this:
const got = require('got');
^
Error [ERR_REQUIRE_ESM]: require() of ES Module
/testwit/node_modules/got/dist/source/index.js from
/testwit/example2.js not supported.
Instead change the require of index.js in /testwit/example2.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/testwit/example2.js:1:13) {
code: 'ERR_REQUIRE_ESM'
}
Made these changes to the declarations at the top of the file and added "type":"module" to the package.json and it worked successfully:
import got from 'got';
import crypto from 'crypto';
import OAuth from 'oauth-1.0a';
import qs from 'querystring';
import rl from 'readline';
const readline = rl.createInterface({
input: process.stdin,
output: process.stdout
});
@SamaaElKomy, I think it might be something with your keys as to why you got that particular error.