.. tabs::
- .. tab:: Authentication
+ .. tab:: API v1.1
- .. literalinclude:: ../examples/authentication.py
+ .. tabs::
- .. tab:: PIN-based Authorization
+ .. tab:: Authentication
- .. literalinclude:: ../examples/pin-based_authorization.py
+ .. literalinclude:: ../examples/API_v1/authentication.py
- .. tab:: Tweet / Update Status
+ .. tab:: PIN-based Authorization
- .. literalinclude:: ../examples/update_status.py
+ .. literalinclude:: ../examples/API_v1/pin-based_authorization.py
- .. tab:: Follow Followers
+ .. tab:: Tweet / Update Status
- .. literalinclude:: ../examples/follow_followers.py
+ .. literalinclude:: ../examples/API_v1/update_status.py
- .. tab:: Handle Rate Limits
+ .. tab:: Follow Followers
- .. literalinclude:: ../examples/rate_limit_handling.py
+ .. literalinclude:: ../examples/API_v1/follow_followers.py
- .. tab:: Streaming
+ .. tab:: Handle Rate Limits
- .. literalinclude:: ../examples/streaming.py
+ .. literalinclude:: ../examples/API_v1/rate_limit_handling.py
+
+ .. tab:: Streaming
+
+ .. literalinclude:: ../examples/API_v1/streaming.py
+
+ .. tab:: API v2
+
+ .. tabs::
+
+ .. tab:: Authentication
+
+ .. literalinclude:: ../examples/API_v2/authentication.py
+
+ .. tab:: Search Recent Tweets
+
+ .. literalinclude:: ../examples/API_v2/search_recent_tweets.py
+
+ .. tab:: Tweet Fields
+
+ .. literalinclude:: ../examples/API_v2/tweet_fields.py
+
+ .. tab:: Expansions
+
+ .. literalinclude:: ../examples/API_v2/expansions.py
+
+ .. tab:: Create Tweet
+
+ .. literalinclude:: ../examples/API_v2/create_tweet.py
--- /dev/null
+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)
+
+# Alternatively, you can provide the consumer key and secret
+client = tweepy.Client(
+ consumer_key=consumer_key, consumer_secret=consumer_secret
+)
+
+# 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
+)
--- /dev/null
+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']}")
--- /dev/null
+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)
--- /dev/null
+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)
--- /dev/null
+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)