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.24k stars 174 forks source link

Can't post tweet using Twitter free developer plan #485

Open matteocoppola opened 1 year ago

matteocoppola commented 1 year ago

Describe the bug I'm following the library docs to authorize and post a tweet but it doesn't work with client.v1.tweet(). I've tried changing it to client.v2.tweet() but Twitter returns: "Authenticating with Unknown is forbidden for this endpoint. Supported authentication types are [OAuth 1.0a User Context, OAuth 2.0 User Context]".

To Reproduce Here's the code snippet I'm using:

const client = new TwitterApi({
    appKey: my_consumerKey,
    appSecret: my_consumerSecret,
    accessToken: my_accessToken,
    accessSecret: my_accessTokenSecret,
  });

  const response = await client.v2.tweet('My tweet text for testing!');

Expected behavior Using the correct credentials the library should allow us to post a tweet correctly.

Is it even possible to use the library to tweet using the free developer plan?

ChrisArchitect commented 1 year ago

the library works fine with free plan.

Just an idea, Check in your developer platform dashboard under User Authentication Settings that you have enabled "read/write" access under oauth 1.0 settings...... so that your keys are allowed to tweet

ry0y4n commented 1 year ago

Same here !

My code, which I run every few days, stopped working after 2023/6/13. My environment is here.

Here is a part of my code.

const client = new TwitterApi({
    appKey: secrets["appKey"],
    appSecret: secrets["appSecret"],
    accessToken: secrets["accessToken"],
    accessSecret: secrets["accessSecret"]
});

const mediaIds = await client.v1.uploadMedia(`${__dirname}/video/ouput.png`);
console.log(mediaIds) # output some number 
await client.v1.tweet(`${title} ${convertedUrl} @YouTubeより`, { media_ids: [mediaIds] });

and I got a error when v1.tweet

U{"error":"Error: Request failed with code 403 - You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product (Twitter code 453)"}%           
alkihis commented 1 year ago

@ry0y4n I think v1 API endpoints available in v2 API have been removed from free plan. Use v2.tweet instead to send a tweet. (but keep v1.uploadMedia, as no replacement in v2 exists)

ry0y4n commented 1 year ago

Wow! Thank you for your information!

v2.tweet() works successfully!

matteocoppola commented 1 year ago

the library works fine with free plan.

Just an idea, Check in your developer platform dashboard under User Authentication Settings that you have enabled "read/write" access under oauth 1.0 settings...... so that your keys are allowed to tweet

I've regenerated all the 4 keys/secrets and in the section "Access Token and Secret" there's the message "Created with Read and Write permissions"

Still, using the code at the original post doesn't work, returning 403 with this error: Authenticating with Unknown is forbidden for this endpoint. Supported authentication types are [OAuth 1.0a User Context, OAuth 2.0 User Context].

What am I missing? Surely something as @ry0y4a was able to use the v2.tweet() function.

As additional context I'm using Node 14 with this library. Maybe the version for 14 differs greatly from the version for Node 18? Even if in package.json the version is 1.15.0 something could be different?

danjiro commented 1 year ago

had to update implementation as well and can confirm updating to client.v2.tweet works using api key and secret (not oauth) Screenshot 2023-06-19 at 1 07 56 pm

If you're attaching media: v2.tweet('message', { media: { media_ids: ['123'] } })

matteocoppola commented 1 year ago

After several tests, I discovered that the secret wasn't being passed correctly to my function.

The library works well! We can close this issue.

massanishi commented 1 year ago

I was using v2 tweet since last month, but it stopped working sometime this week with the 403 error.

My issue was I had to include the App, which was standalone, inside the Project in the developer portal. All these Twitter restructuring jargons, sigh.

ChrisArchitect commented 1 year ago

"Authenticating with Unknown...."

It was in the error message all along, should have started there with debug before jumping to "the entire library is faulty".

Lessons for next time!

hilmanski commented 1 year ago

thanks! using v2 work for me

arantespp commented 1 year ago

In case it's useful for someone, my app only worked after changing this project "Project use" to "Making a Bot"

image

hitwill commented 9 months ago

the library works fine with free plan. Just an idea, Check in your developer platform dashboard under User Authentication Settings that you have enabled "read/write" access under oauth 1.0 settings...... so that your keys are allowed to tweet

I've regenerated all the 4 keys/secrets and in the section "Access Token and Secret" there's the message "Created with Read and Write permissions"

Still, using the code at the original post doesn't work, returning 403 with this error: Authenticating with Unknown is forbidden for this endpoint. Supported authentication types are [OAuth 1.0a User Context, OAuth 2.0 User Context].

What am I missing? Surely something as @ry0y4a was able to use the v2.tweet() function.

As additional context I'm using Node 14 with this library. Maybe the version for 14 differs greatly from the version for Node 18? Even if in package.json the version is 1.15.0 something could be different?

After enabling read/write - regenerating the tokens worked

This needs to be in the documentation


import Twitter from 'twitter-api-v2';

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

export function tweet(tweet: string) {
  client.v2.tweet(tweet);
}
`
mathix420 commented 4 months ago

In my case I had to wait for a couple of minutes before it worked.