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

[bug] v1 media upload not working

Open haupt-pascal opened this issue 1 year ago • 1 comments

Twitter API v2 Media Upload Authentication Issue

Description

When trying to upload media using v1 API with OAuth 1.0a app credentials while posting with OAuth 2.0 user token, getting 401 authentication errors.

Environment

  • node-twitter-api-v2: latest
  • Node.js: 18.x
  • TypeScript: 5.x

Code

const v1Client = new TwitterApi({
    appKey: process.env.TWITTER_API_KEY,
    appSecret: process.env.TWITTER_API_SECRET,
    accessToken: process.env.TWITTER_ACCESS_TOKEN,
    accessSecret: process.env.TWITTER_ACCESS_SECRET,
});

const v2Client = new TwitterApi(connection.access_token); // OAuth 2.0 user token

try {
    const mediaId = await v1Client.v1.uploadMedia(imageBuffer, {
        mimeType: "image/jpeg",
        additionalOwners: [connection.member_id],
    });

    return await v2Client.v2.tweet({
        text: content,
        media: { media_ids: [mediaId] }
    });
} catch (error) {
    console.error("Error:", error);
}

Error:

Twitter media upload error: { ERROR Twitter media upload error: { errors: [ { message: 'Could not authenticate you', code: 32 } ] } ERROR Twitter API error: Media upload failed: Request failed with code 401 - Could not authenticate you (Twitter code 32)

haupt-pascal avatar Dec 27 '24 06:12 haupt-pascal

I've been using the v1.uploadMedia for ~2 years and it works fine

Odds are this error is legitimate and you need to figure out what's going on with auth that you're potentially doing incorrectly

francistogram avatar Feb 07 '25 16:02 francistogram

Indeed, logging in with a bearer token like you did can only five read-only access, so you can't tweet with it.

plhery avatar Sep 04 '25 21:09 plhery