Saturday, 17 December 2022

How to get a notification when a tweet is deleted via the twitter API?

How can I get a notice when a tweet was deleted using the twitter API?

In version 1.0 of the API, I was able to get the notification in a stream using this:

var Twit = require("twit");

var T = new Twit({
    consumer_key: "555",
    consumer_secret: "555",
    access_token: "555",
    access_token_secret: "555",
    timeout_ms: 60 * 1000,
    strictSSL: true
});

var userIds = [ "123", "456" ];

var stream = T.stream("statuses/filter", { follow: userIds.join(",") });

stream.on("delete", (x) => console.log("Tweet was deleted", x));

However, without notice. The deleted events stopped being streamed.

So now I'm trying to do it with v2 of the twitter API like this:

const BEARER_TOKEN = "555";

const { ETwitterStreamEvent, TweetStream, TwitterApi, ETwitterApiError } = require("twitter-api-v2");

const appClient = new TwitterApi(BEARER_TOKEN);

const stream = await appClient.v2.getStream("tweets/compliance/stream", { partition: 1 });

stream.on(ETwitterStreamEvent.Data, (x) => console.log("Got data", x));

The call to getStream() throws the following error:

data: {
    client_id: '555',
    detail: 'When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal.',
    registration_url: 'https://developer.twitter.com/en/docs/projects/overview',
    title: 'Client Forbidden',
    required_enrollment: 'Standard Basic',
    reason: 'client-not-enrolled',
    type: 'https://api.twitter.com/2/problems/client-forbidden'
}

I also tried using an app only login such as this:

const TWITTER_CLIENT = new TwitterApi({
    appKey: CONSUMER_KEY,
    appSecret: CONSUMER_SECRET,
    accessToken: ACCESS_TOKEN,
    accessSecret: ACCESS_TOKEN_SECRET
});

var appClient = await TWITTER_CLIENT.appLogin();

That throws the same error as above.

Using 2.0's getStream() with /tweets/search/stream/ does return an event a tweet is created, but not when it is deleted. It also has a limited query with only 5 rules per stream and rules are only 512 characters in length. Which won't cover all the screen names I currently track in the 1.0 version of the API.

I also tried using compliance jobs, but it takes a very long time and ends up returning an empty array anyways instead of any info about the tweet ids I provided:

var job = await appClient.v2.sendComplianceJob({
    type: "tweets",
    ids:[
// the ids are not from my dev account or from 
// a account that authed my app
        "555", // id of tweet I deleted
        "123", // id of tweet I deleted
        "456", // id of tweet I didn't delete
    ]
});

// takes 5-10 minutes to say its complete
var jobResults = await appClient.v2.complianceJobResult(job.data);

// echos: jobResults: []
console.log("jobResults", jobResults);

How can I get a stream event of when a tweet is deleted (of any specific user I choose) using the v2 API of twitter?



from How to get a notification when a tweet is deleted via the twitter API?

No comments:

Post a Comment