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

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
)

Related

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

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

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!

Connect to Twitter given already have token?

I have looked at all the Python Twitter API wrappers that I could find on Bitbucket, Github and PyPi, but have been unable to find one which allows you to connect to Twitter if you already have the authentication token.
I am aware that I can generate the authentication token using an OAuth token, OAuth token secret, Twitter Token and Twitter Secret; but I would like to skip that processing + not prompt users who already have accounts.
The tweepy library seems popular; but lacks documentation...
Would someone be able to show me a tweet postage which uses Tweepy (or any other Python Twitter library) that uses only the authentication token?
EDIT: I ended up getting to work right with Twython.
You have to store the access token and secret returned by the provider after authentication and use them in the subsequent requests to read or write. I have been using rauth (https://github.com/litl/rauth) and highly recommend it.
EDIT
Assuming you have already have a valid access token and a secret you can create a service object and read or write data using the twitter API (skipping the authentication steps). I have included the necessary steps from the rauth documentation below:
twitter = OAuth1Service(
name='twitter',
consumer_key='YOUR_CONSUMER_KEY',
consumer_secret='YOUR_CONSUMER_SECRET',
request_token_url='https://api.twitter.com/oauth/request_token',
access_token_url='https://api.twitter.com/oauth/access_token',
authorize_url='https://api.twitter.com/oauth/authorize',
header_auth=True)
params = {'include_rts': 1, # Include retweets
'count': 10} # 10 tweets
response = twitter.get('https://api.twitter.com/1/statuses/home_timeline.json',
params=params,
access_token=access_token,
access_token_secret=access_token_secret,
header_auth=True)

Sending out twitter retweets with Python

I wanted to know if this was possible- I want to use Python to retweet every tweet a person sends out. If yes then how can I implement this?
Unfortunately, python-twitter does not yet support the Twitter Retweet REST call.
You'll have to make that call directly yourself (using direct calls to api._FetchURL) or apply the patch in issue 130 to add support.
You're better off with using tweepy; read the API documentation, there is a handy retweet(id) method for retweeting.
Quick and dirty example:
import tweepy
auth = tweepy.BasicAuthHandler("username", "password")
api = tweepy.API(auth)
for status in api.user_timeline('someuser'):
api.retweet(status.id)
This will retweet the last 20 statuses from someuser. You'll want to do some more coding to prevent it from retweeting those same messages again next time you run the script though.
Edit: Twitter no longer accepts BasicAuth authentication, and you'll have to use the OAuth authentication exchange to get a authorisation token. Changing the example above to use OAuth would detract from the retweet API point I was trying to make, see the Tweepy OAuth tutorial for an extensive tutorial.
It's possible to retweet anything the people you follow tweet.You can also retweet all of the public tweets.
Use this link : https://github.com/joshthecoder/tweepy
you will know how to do it in a very simple way .
Here's the OAuth "Quick and dirty" method, please keep in mind that you will need to have Tweepy installed for this to work.
import tweepy
api_key = 'your_key'
api_secret = 'your_secret_key'
access_token = 'your_token'
access_secret = 'your_secret_token'
auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
for status in api.user_timeline('someuser'):
api.retweet(status.id)
The newest version of python-twitter allows you to retweet with the command
api.PostRetweet(tweet_id)
where api is a logged-in api and tweet_id is the id of the tweet you want to retweet.

Categories

Resources