Why can't I delete multiple tweets with this code? Python tweepy - python

I am trying to use this code to delete 550 tweets from my account, as I want to use it as my personal account again without the tweets from my bots:
import tweepy
import json
auth = tweepy.OAuthHandler("consumer_key", "consumer_secret")
auth.set_access_token("access_token", "access_token_secret")
api = tweepy.API(auth)
screen_name = "YOUR_TWITTER_HANDLE"
timelines = api.user_timeline(screen_name, count = 550, include_rts = False)
for status in timelines:
tweetid = status._json
api.destroy_status(tweetid)
But every time I run it, I get this error:
tweepy.error.TweepError: [{'message': 'Application cannot perform write actions. Contact Twitter Platform Operations through https://support.twitter.com/forms/platform', 'code': 261}]
When I print tweetid, it is successfully grabbing all my tweets. I can also tweet normally using api.update_status, so why is this not working? What am I doing wrong?

seems like you have made to many requests to the twitter api - from my little reasearch the error code 261 means the token was suspended. Check this links:
Why is the twitter API throwing this error?
https://blog.cotten.io/common-twitter-error-codes-6b324396042e

It sounds like your API key has been restricted (this can happen if you try to perform too many automated actions, see the automation rules https://help.twitter.com/en/rules-and-policies/twitter-automation). You will need to have the app restored via https://help.twitter.com/forms/automation

Related

From yesterday i'm facing the issue of snscrape with twitter

ScraperException: 4 requests to https://api.twitter.com/2/search/adaptive.json?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_ext_alt_text=true&include_quote_count=true&include_reply_count=1&tweet_mode=extended&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&send_error_codes=true&simple_quoted_tweets=true&q=%28from%3AZeeNewsEnglish%29+until%3A2023-01-12+since%3A2023-01-08+-filter%3Areplies&count=100&query_source=spelling_expansion_revert_click&pc=1&spelling_corrections=1&ext=mediaStats%2ChighlightedLabel failed, giving up.
I tried following code :
import snscrape.modules.twitter as sntwitter
import time
query5 = "(from:BBC) until:2023-01-12 since:2023-01-08 -filter:replies"
news = [query5]
tweets = []
for news_data in news:
limit = 500
for tweet in sntwitter.TwitterSearchScraper(news_data).get_items():
# print(vars(tweet))
# break
if len(tweets) == limit:
break
else:
tweets.append([tweet.date, tweet.username, tweet.content])
time.sleep(2)
you have to install the last version of snscrape 0.5.0.20230113.
Faced the same issue. I guess snscraper, made use of Twitter API (elevated access) in the backend. Twitter shut down all the bots which were fetching the API data. Twitter essentially wants you to make authentic use of their data. I suggest signing up on twitter's developer account and requesting the elevated environment. Notice the first line in error makes a call to Twitter API.

Twitter premium not authorized

I attempted to run the code below and am getting an error that states:
HTTP Error code: 403: Forbidden: Authentication succeeded but account is not authorized to access this resource.
from searchtweets import ResultStream, gen_rule_payload, load_credentials, collect_results
import requests
premium_search_args = load_credentials("/home/dirname/twitter_keys.yaml",
yaml_key="search_tweets_premium",
env_overwrite=False)
rule = gen_rule_payload("basketball", results_per_call=100) # testing with a sandbox account
print(rule)
from searchtweets import collect_results
tweets = collect_results(rule,
max_results=100,
result_stream_args=premium_search_args)
# print(tweets.all_text)
[print(tweet.all_text, end='\n\n') for tweet in tweets[0:10]];
My YAML file looks like this:
search_tweets_premium:
account_type: premium
endpoint: https://api.twitter.com/1.1/tweets/search/fullarchive/dev.json
consumer_key: AAAAAAAAAAAAAAAAAAAAA
consumer_secret: BBBBBBBBBBBBBBBBBBBBBBBBBBB
Only other thing to note is that I am using the free/sandbox service.
Any ideas if I am doing anything wrong in the code, the YAML, and/or within my Twitter developer account?
You'll need to go to https://developer.twitter.com/en/account/environments
There you should be able to see the various development environments that you have. You can create one should they not have been created.
The dev environment label would then be the thing you use to replace in your endpoint.
In my example, it would be:
https://api.twitter.com/1.1/tweets/search/fullarchive/development.json
If that still doesn't work, you might need to include a bearer token in your YAML file.

Is there a way to call a custom Twitter endpoint using Tweepy?

I'm using Tweepy to pull some data from Twitter in Python. I'd like to pull data from the statuses/retweeters/ids endpoint, however, it doesn't look like Tweepy has this endpoint built-in.
Is there a way I can call a custom endpoint so that I can use this endpoint and not just the API.retweets() endpoint in Tweepy that pulls retweets? I'm looking to grab individual user IDs of people who retweeted a specific tweet.
Thanks!
Looking at tweepy library code and how API methods are defined, I guess you could try this:
import tweepy
from tweepy.binder import bind_api
# OAuth process
auth = tweepy.OAuthHandler('key', 'secret')
auth.secure = True
auth.set_access_token('token', 'token_secret')
# Creation of the interface
api = tweepy.API(auth)
# Call of the custom endpoint
bind_api(
api=api,
path='/statuses/retweeters/ids',
method='GET',
allowed_param=['id', 'count', 'cursor', 'stringify_ids'],
require_auth=True
)

Error posting a tweet via python TwitterAPI

I'm trying to post a tweet via TwitterApi, using oAuth2 and I get error
'u'{"errors":[{"code":220,"message":"Your credentials do not allow access to this resource."}]}'
My code is as follows:
from TwitterAPI import TwitterAPI
CONSUMER_KEY = 'xyz'
CONSUMER_SECRET = 'xyz'
def tweet_it():
api = TwitterAPI(CONSUMER_KEY, CONSUMER_SECRET, auth_type='oAuth2')
r = api.request('statuses/update', {'status': 'Hello World!'})
pass
if __name__ == "__main__":
tweet_it()
I have set my app account permissions to 'Read, Write and Access direct messages' and regenerate consumer keys. What am I missing?
I could search tweets this way without any issues.
I could post and search using oAuth1.
I've found out the solution or better say what is the issues.
Behavior described in my questions is correct as I'm using Application-only authentication and with this it's not allowed to access certain API calls, see https://dev.twitter.com/oauth/application-only.
Following twitter api documentation
for calling my account only, it's best way to go with oAuth1 and specify consumer key and access token and corresponding secrets.
for calling it from 3rd party application, you should go with 3-legged authorization, see https://dev.twitter.com/oauth/3-legged.

Obtain all followers_ids of a user from twitter using correct pagination

I am very new to using the tweepy api and have managed to get the followers_ids for a particular unauthenticated twitter user. I would now like to know how to get all the follower_ids for the twitter user as the first call only gives me 5000 ids and the user has a much larger set of followers. I have gone through the tweepy documentation but am still quite unclear about how to actually perform pagination using tweepy cursor. I would really appreciate a simple explanation to how to perform pagination and some help with my current code to perform the aforementioned task of obtaining all followers_ids of a twitter user.
import tweepy
user = tweepy.api.get_user('someuser')
cursors = tweepy.Cursor(user.followers_ids, id='screen_name')
for cursor in cursors.items():
print cursor.screen_name
one error that I am getting while using this is the following:
tweepy.error.TweepError: This method does not perform pagination
any help would be greatly appreciated.
I think you need need to have an authenticated tweepy.API instance first. I got the same error when I tried
user = api.get_user('username')
c = tweepy.Cursor(user.follower_ids)
However, this works for me:
import tweepy
## first set up authenticated API instance
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_secret)
api = tweepy.API(auth)
for block in tweepy.Cursor(api.followers_ids, 'username').items():
## do something with the block of 5000 follower ids
Hope that helps!

Categories

Resources