Examples¶
import tweepy
# Your app's API/consumer key and secret can be found under the Consumer Keys
# section of the Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
consumer_key = ""
consumer_secret = ""
# Your account's (the app owner's account's) access token and secret for your
# app can be found under the Authentication Tokens section of the
# Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
access_token = ""
access_token_secret = ""
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
api = tweepy.API(auth)
# If the authentication was successful, this should print the
# screen name / username of the account
print(api.verify_credentials().screen_name)
import tweepy
# PIN-based OAuth
# https://developer.twitter.com/en/docs/authentication/oauth-1-0a/pin-based-oauth
# Your app's API/consumer key and secret can be found under the Consumer Keys
# section of the Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
consumer_key = ""
consumer_secret = ""
auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret)
# This prints a URL that can be used to authorize your app
# After granting access to the app, a PIN to complete the authorization process
# will be displayed
print(auth.get_authorization_url())
# Enter that PIN to continue
verifier = input("PIN: ")
auth.get_access_token(verifier)
api = tweepy.API(auth)
# If the authentication was successful, this should print the
# screen name / username of the account
print(api.verify_credentials().screen_name)
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
api = tweepy.API(auth)
# Tweet / Update Status
# The app and the corresponding credentials must have the Write permission
# Check the App permissions section of the Settings tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
# Make sure to reauthorize your app / regenerate your access token and secret
# after setting the Write permission
api.update_status("This Tweet was Tweeted using Tweepy!")
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
api = tweepy.API(auth)
# Follow every follower of the authenticated user
for follower in tweepy.Cursor(api.get_followers).items():
follower.follow()
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
# Setting wait_on_rate_limit to True when initializing API will initialize an
# instance, called api here, that will automatically wait, using time.sleep,
# for the appropriate amount of time when a rate limit is encountered
api = tweepy.API(auth, wait_on_rate_limit=True)
# This will search for Tweets with the query "Twitter", returning up to the
# maximum of 100 Tweets per request to the Twitter API
# Once the rate limit is reached, it will automatically wait / sleep before
# continuing
for tweet in tweepy.Cursor(api.search_tweets, "Twitter", count=100).items():
print(tweet.id)
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# Subclass Stream to print IDs of Tweets received
class IDPrinter(tweepy.Stream):
def on_status(self, status):
print(status.id)
# Initialize instance of the subclass
printer = IDPrinter(
consumer_key, consumer_secret,
access_token, access_token_secret
)
# Filter realtime Tweets by keyword
printer.filter(track=["Twitter"])
import tweepy
# Your app's bearer token can be found under the Authentication Tokens section
# of the Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
bearer_token = ""
# Your app's API/consumer key and secret can be found under the Consumer Keys
# section of the Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
consumer_key = ""
consumer_secret = ""
# Your account's (the app owner's account's) access token and secret for your
# app can be found under the Authentication Tokens section of the
# Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
access_token = ""
access_token_secret = ""
# You can authenticate as your app with just your bearer token
client = tweepy.Client(bearer_token=bearer_token)
# You can provide the consumer key and secret with the access token and access
# token secret to authenticate as a user
client = tweepy.Client(
consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_token_secret
)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Search Recent Tweets
# This endpoint/method returns Tweets from the last seven days
response = client.search_recent_tweets("Tweepy")
# The method returns a Response object, a named tuple with data, includes,
# errors, and meta fields
print(response.meta)
# In this case, the data field of the Response returned is a list of Tweet
# objects
tweets = response.data
# Each Tweet object has default id and text fields
for tweet in tweets:
print(tweet.id)
# By default, this endpoint/method returns 10 results
# You can retrieve up to 100 Tweets by specifying max_results
response = client.search_recent_tweets("Tweepy", max_results=100)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# You can specify additional Tweet fields to retrieve using tweet_fields
response = client.search_recent_tweets(
"Tweepy", tweet_fields=["created_at", "lang"]
)
tweets = response.data
# You can then access those fields as attributes of the Tweet objects
for tweet in tweets:
print(tweet.id, tweet.lang)
# Alternatively, you can also access fields as keys, like a dictionary
for tweet in tweets:
print(tweet["id"], tweet["lang"])
# There’s also a data attribute/key that provides the entire data dictionary
for tweet in tweets:
print(tweet.data)
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# You can specify expansions to retrieve additional objects that relate to the
# returned results
response = client.search_recent_tweets(
"Tweepy", expansions=["attachments.media_keys", "author_id"]
)
tweets = response.data
# You can then access those objects in the includes Response field
includes = response.includes
users = includes["users"]
# The IDs that represent the expanded objects are included directly in the
# returned data objects
for tweet in tweets:
print(tweet.author_id)
# An efficient way of matching expanded objects to each data object is to
# create a dictionary of each type of expanded object, with IDs as keys
users = {user["id"]: user for user in users}
for tweet in tweets:
print(tweet.id, users[tweet.author_id].username)
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
client = tweepy.Client(
consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_token_secret
)
# Create Tweet
# The app and the corresponding credentials must have the Write permission
# Check the App permissions section of the Settings tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
# Make sure to reauthorize your app / regenerate your access token and secret
# after setting the Write permission
response = client.create_tweet(
text="This Tweet was Tweeted using Tweepy and Twitter API v2!"
)
print(f"https://twitter.com/user/status/{response.data['id']}")