Add API v2 examples
authorHarmon <Harmon758@gmail.com>
Sun, 2 Jan 2022 00:29:34 +0000 (18:29 -0600)
committerHarmon <Harmon758@gmail.com>
Sun, 2 Jan 2022 00:29:34 +0000 (18:29 -0600)
12 files changed:
docs/examples.rst
examples/API_v1/authentication.py [moved from examples/authentication.py with 100% similarity]
examples/API_v1/follow_followers.py [moved from examples/follow_followers.py with 100% similarity]
examples/API_v1/pin-based_authorization.py [moved from examples/pin-based_authorization.py with 100% similarity]
examples/API_v1/rate_limit_handling.py [moved from examples/rate_limit_handling.py with 100% similarity]
examples/API_v1/streaming.py [moved from examples/streaming.py with 100% similarity]
examples/API_v1/update_status.py [moved from examples/update_status.py with 100% similarity]
examples/API_v2/authentication.py [new file with mode: 0644]
examples/API_v2/create_tweet.py [new file with mode: 0644]
examples/API_v2/expansions.py [new file with mode: 0644]
examples/API_v2/search_recent_tweets.py [new file with mode: 0644]
examples/API_v2/tweet_fields.py [new file with mode: 0644]

index 541a3ca9bb6dc9a70f02ab14e4d874082d3a69a4..c56406612ef81b29ef4d83ca0ccc3bf00c547132 100644 (file)
@@ -6,26 +6,54 @@ Examples
 
 .. 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
diff --git a/examples/API_v2/authentication.py b/examples/API_v2/authentication.py
new file mode 100644 (file)
index 0000000..f352cfe
--- /dev/null
@@ -0,0 +1,37 @@
+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
+)
diff --git a/examples/API_v2/create_tweet.py b/examples/API_v2/create_tweet.py
new file mode 100644 (file)
index 0000000..bf671da
--- /dev/null
@@ -0,0 +1,28 @@
+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']}")
diff --git a/examples/API_v2/expansions.py b/examples/API_v2/expansions.py
new file mode 100644 (file)
index 0000000..358ad00
--- /dev/null
@@ -0,0 +1,28 @@
+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)
diff --git a/examples/API_v2/search_recent_tweets.py b/examples/API_v2/search_recent_tweets.py
new file mode 100644 (file)
index 0000000..377d2f6
--- /dev/null
@@ -0,0 +1,27 @@
+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)
diff --git a/examples/API_v2/tweet_fields.py b/examples/API_v2/tweet_fields.py
new file mode 100644 (file)
index 0000000..f8d1ef2
--- /dev/null
@@ -0,0 +1,24 @@
+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)