I am trying to download tweets by a specific user from Twitter API v2 using Tweepy
. For some reason, when I add organic_metrics
to tweet_fields
I get an empty response. If I remove organic_metrics
I receive tweets as a response as expected.
Here is an MWE.
import configparser
from pprint import PrettyPrinter
import tweepy
import logging
# Read Twitter authentication information from settings.ini
config = configparser.RawConfigParser()
config.read("settings.ini")
logging.basicConfig(level=logging.INFO)
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)
pp = PrettyPrinter()
# Setup access to API
def connect_to_twitter_OAuth():
client = tweepy.Client(
consumer_key=config["AUTHENTICATION"]["CONSUMER_KEY"],
consumer_secret=config["AUTHENTICATION"]["CONSUMER_SECRET"],
access_token=config["AUTHENTICATION"]["ACCESS_TOKEN"],
access_token_secret=config["AUTHENTICATION"]["ACCESS_SECRET"],
wait_on_rate_limit=True,
)
_logger.debug("Authenticated with Twitter with user context.")
return client
# Create API object
api = connect_to_twitter_OAuth()
tweet_fields = [
"id",
"text",
"conversation_id",
"created_at",
"in_reply_to_user_id",
"organic_metrics",
]
paginator = tweepy.Paginator(
api.get_users_tweets,
user_id,
max_results=5,
tweet_fields=tweet_fields,
user_auth=True,
)
_logger.debug("Retrieving tweets from user.")
for tweet in paginator.flatten(limit=250):
pp.pprint(tweet)
_logger.debug("Tweet retrieved.")
Any thoughts?
"Non-public, organic, and promoted metrics are only available for Tweets that have been created within the last 30 days."
https://developer.twitter.com/en/docs/twitter-api/metrics
This is why you cannot paginate through responses. Hope this answers some of your confusion!