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

Send a Tweet with a Media not working: 400 - 'Your media IDs are invalid.'

Open antoinegues opened this issue 3 years ago • 1 comments

I have problem for send a tweet with a media

There is my code :

`async function sendTweet(tweetID: string) {

await twitterClient.v1.uploadMedia(__dirname + "/../test.mp4", {
    mimeType: EUploadMimeType.Mp4,
}).then(async (data) => {
    const detail = (await twitterClient.v1.mediaInfo(data));
    console.log(detail);
    twitterClient.v2.reply('Test', tweetID, {
        media: {media_ids: [detail.media_id_string]}
    });
});

}`

And the error :

/Users/antoine/Documents/Projet/test/node_modules/twitter-api-v2/dist/client-mixins/request-handler.helper.js:103 return new types_1.ApiResponseError(errorString, { ^ ApiResponseError: 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) at RequestHandlerHelper.createResponseError (/Users/antoineguespin/Documents/Projet/test/node_modules/twitter-api-v2/dist/client-mixins/request-handler.helper.js:103:16) at RequestHandlerHelper.onResponseEndHandler (/Users/antoineguespin/Documents/Projet/test/node_modules/twitter-api-v2/dist/client-mixins/request-handler.helper.js:252: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: 400, headers: { date: 'Thu, 22 Sep 2022 20:39:11 UTC', server: 'tsa_f', 'set-cookie': [ 'guest_id=v1%3A166387915124632486; Max-Age=34214400; Expires=Mon, 23 Oct 2023 20:39:11 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None' ], 'api-version': '2.49', 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-cache, no-store, max-age=0', 'content-length': '208', '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': '1663879632', 'content-disposition': 'attachment; filename=json.json', 'x-content-type-options': 'nosniff', 'x-rate-limit-remaining': '194', 'strict-transport-security': 'max-age=631138519', 'x-response-time': '437', 'x-connection-hash': '7b8a0c793e806bc1055da8e5c93fc337550d26c86b486d1f57510115799caa67', connection: 'close' }, rateLimit: { limit: 200, remaining: 194, reset: 1663879632 }, data: { errors: [ [Object] ], title: 'Invalid Request', detail: 'One or more parameters to your request was invalid.', type: 'https://api.twitter.com/2/problems/invalid-request' }, errors: [ { parameters: [Object], message: 'Your media IDs are invalid.' } ] }

Thanks in advance

antoinegues avatar Sep 22 '22 20:09 antoinegues

Hi, Can you give more details by accessing error.errors?

try {
  await client.v2.tweet(...)
} catch (error) {
  console.log(error.errors)
}

alkihis avatar Sep 23 '22 06:09 alkihis

After more research i found the problem come from the twitter API himself, thank for the helping

antoinegues avatar Sep 24 '22 13:09 antoinegues

hey @antoinegues I am also building a project where I want to post media using twitter-api-v2. if you have successfully posted media using Twitter-api-v2, Can you please share your code or plz tell me which endpoints you have used white posting media? I also tried and read the documentation but I am not getting the media_id and getting error in response?

sanyamjain04 avatar Oct 12 '22 07:10 sanyamjain04

All the code you need is in my first message, but check your media is compatible with twitter. You can test on send it on twitter with your browser. If you need more help, don't hesitate to ask.

antoinegues avatar Oct 12 '22 08:10 antoinegues

Hello @antoinegues, Sorry to bother you again, Can you please tell me what I'm doing wrong here? I am able to post Tweets, like ,unlike but not able to uploadMedia. Here is my code:

const TwitterApi = require("twitter-api-v2").default;

const twitterClient = new TwitterApi({
  clientId: process.env.CLIENT_ID,
  clientSecret: process.env.CLIENT_SECRET,
});

const getRefreshClient = async () => {
  // take the old accessToken and refreshToken from DB
  let { data: tokens, error } = await supabase
    .from("tokens")
    .select("*")
    .single();
  const { Refresh_Tokens } = tokens;

  // Generate new Refresh token using old refresh token
  const {
    client: refreshedClient,
    accessToken,
    refreshToken: newRefreshToken,
  } = await twitterClient.refreshOAuth2Token(Refresh_Tokens);

  // Update the refresh accessToken and newRefreshToken in DB
  const { data: updatedData, error: error1 } = await supabase
    .from("tokens")
    .update({ Access_tokens: accessToken, Refresh_Tokens: newRefreshToken })
    .eq("id", "1")
    .single();

  return refreshedClient;
};

const uploadMedia = async () => {
    
      const refreshedClient = await getRefreshClient();
    try {
        const  data = await refreshedClient.v1.uploadMedia( './SanyamJain.jpg')
        console.log(data);
      } catch (error) {
        console.log(error);
      }
}
uploadmedia();

It is showing this error

D:\open source\New folder\kubetweet\backend\node_modules\twitter-api-v2\dist\client-mixins\request-handler.helper.js:103
        return new types_1.ApiResponseError(errorString, {
               ^

ApiResponseError: Request failed with code 400
    at RequestHandlerHelper.createResponseError (D:\open source\New folder\kubetweet\backend\node_modules\twitter-api-v2\dist\client-mixins\request-handler.helper.js:103:16)
    at RequestHandlerHelper.onResponseEndHandler (D:\open source\New folder\kubetweet\backend\node_modules\twitter-api-v2\dist\client-mixins\request-handler.helper.js:252:25)
    at Gunzip.emit (node:events:527:28)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {  
  error: true,
  type: 'response',
  code: 400,
  headers: {
    date: 'Sun, 30 Oct 2022 08:04:50 GMT',
    perf: '7626143928',
    server: 'tsa_k',
    'set-cookie': [
      'guest_id_marketing=v1%3A166711709044688527; Max-Age=63072000; Expires=Tue, 29 Oct 2024 08:04:50 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',
      'guest_id_ads=v1%3A166711709044688527; Max-Age=63072000; Expires=Tue, 29 Oct 2024 08:04:50 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',  
      'personalization_id="v1_QDkSQ15TW3DBjcFhiGPRRQ=="; Max-Age=63072000; Expires=Tue, 29 Oct 2024 08:04:50 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None',
      'guest_id=v1%3A166711709044688527; Max-Age=63072000; Expires=Tue, 29 Oct 2024 08:04:50 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None'       
    'x-response-time': '179',
    'x-connection-hash': '90ad247945fa2388f30885d823e2c28832db63d3dd5081a4aa2daf0b1785562a',
    connection: 'close'
  },
  rateLimit: undefined,
  data: {
    error: 'invalid_request',
    error_description: 'Value passed for the token was invalid.',
    errors: [ { code: 131, message: 'invalid_request' } ]
  }
}

sanyamjain04 avatar Oct 30 '22 08:10 sanyamjain04

Have you try to upload your media with the web client and tweet it ?

antoinegues avatar Oct 31 '22 21:10 antoinegues

Yes, I tweeted the same image in WebClient. it is working fine, Twitter supports jpg images.

sanyamjain04 avatar Nov 03 '22 00:11 sanyamjain04